home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / scheme / schematk / src_1152.lha / Main.subproj / Main.m < prev    next >
Encoding:
Text File  |  1993-02-25  |  13.0 KB  |  451 lines

  1. /* Copyright รก 1991 Gustavus Adolphus College.  All rights reserved.
  2.  *
  3.  * Schematik was developed by Gustavus Adolphus College (GAC) with
  4.  * support from NeXT Computer, Inc.  Permission to copy this software,
  5.  * to redistribute it, and to use it for any purpose is granted,
  6.  * subject to the following restrictions and understandings.
  7.  *
  8.  * 1. Any copy made of this software must include this copyright
  9.  * notice in full.
  10.  *
  11.  * 2. Users of this software agree to make their best efforts (a) to
  12.  * return to the GAC Mathematics and Computer Science Department any
  13.  * improvements or extensions that they make, so that these may be
  14.  * included in future releases; and (b) to inform GAC of noteworthy
  15.  * uses of this software.
  16.  *
  17.  * 3. All materials developed as a consequence of the use of this
  18.  * software shall duly acknowledge such use, in accordance with the
  19.  * usual standards of acknowledging credit in academic research.
  20.  *
  21.  * 4. GAC makes no express or implied warranty or representation of
  22.  * any kind with respect to this software, including any warranty
  23.  * that the operation of this software will be error-free.  ANY
  24.  * IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
  25.  * PURPOSE IS HEREBY DISCLAIMED.  GAC is under no obligation to
  26.  * provide any services, by way of maintenance, update, or otherwise.
  27.  *
  28.  * 5. In conjunction with products arising from the use of this
  29.  * material, there shall be no use of the name of Gustavus Adolphus
  30.  * College nor of any adaptation thereof in any advertising,
  31.  * promotional, or sales literature without prior written consent
  32.  * from GAC in each case.
  33.  */
  34.  
  35. #import "Main.h"
  36. #import "../defines.h"
  37. #import InfoAgent_h
  38. #import HelpAgent_h
  39. #import PrefAgent_h
  40. #import FindAgent_h
  41. #import InteractionWin_h
  42. #import DocText_h
  43. #import SchemeProtocol_h
  44. #import GraphicsView_h
  45. #import <appkit/FontManager.h>
  46. #import <appkit/Listener.h>
  47. #import <appkit/Speaker.h>
  48. #import <appkit/Matrix.h>
  49. #import <appkit/NXCursor.h>
  50. #import <appkit/OpenPanel.h>
  51. #import <appkit/PrintInfo.h>
  52. #import <appkit/ScrollView.h>
  53. #import <appkit/Text.h>
  54. #import <appkit/publicWraps.h>
  55. #import <dpsclient/wraps.h>
  56. #import <objc/HashTable.h>
  57. #import <objc/List.h>
  58. #import <sys/signal.h>
  59. #import <mach.h>
  60. extern void NXBeep(void);
  61.  
  62. #define ALTARROWCURSORTIFF    "altcursor1"
  63. #define ALTIBEAMCURSORTIFF    "altcursor1"
  64. #define ALTCURSORHOTSPOT    {7,7}
  65. #define NEW_ROTATENUM        5
  66.  
  67. static int numDocsOpened=0;
  68. static BOOL terminus=NO;
  69.  
  70. @implementation Main
  71.  
  72. + new
  73. {
  74. DEBUG_FUNC1(DEBUGLEVEL);
  75.     self = [super new];
  76.     prefAgent = [PrefAgent new];
  77.     if ([prefAgent useSpecialCursors])
  78.       {
  79.         NXPoint p=ALTCURSORHOTSPOT;
  80.     NXArrow = [[NXCursor alloc] initFromImage:[[NXImage alloc] initFromSection:ALTARROWCURSORTIFF]];
  81.     [NXArrow setHotSpot:&p];
  82.     NXIBeam = [[NXCursor alloc] initFromImage:[[NXImage alloc] initFromSection:ALTIBEAMCURSORTIFF]];
  83.     [NXIBeam setHotSpot:&p];
  84.       }
  85.     interactionWindow = [InteractionWin new];
  86.     [interactionWindow makeKeyAndOrderFront:self];
  87.     protocolObj = [[SchemeProtocol allocFromZone:[self zone]] init];
  88.     infoAgent = [InfoAgent new];
  89.     helpAgent = [HelpAgent new];
  90.     findAgent = [FindAgent new];
  91.     [protocolObj showRunState:INPUTSTATE];
  92.     [[NXApp printInfo] setOrientation:NX_PORTRAIT andAdjust:YES];
  93.     [[NXApp printInfo] setHorizPagination:NX_FITPAGINATION];
  94.     [[NXApp printInfo] setHorizCentered:NO];
  95.     [[NXApp printInfo] setVertCentered:NO];
  96.     [FontManager setFontPanelFactory:[MyFontPanel class]];
  97.     graphicsZone = NXCreateZone(vm_page_size, vm_page_size, YES);
  98.     graphicsViews = [[HashTable allocFromZone:graphicsZone] initKeyDesc:"i" valueDesc:"@"];
  99.     return self;
  100. }
  101.  
  102. - info:sender
  103. {
  104. DEBUG_FUNC1(DEBUGLEVEL);
  105.     return [infoAgent activateAgent:[sender selectedCell]];    
  106. }
  107.  
  108. - help:sender
  109. {
  110. DEBUG_FUNC1(DEBUGLEVEL);
  111.     return [helpAgent activateAgent:[sender selectedCell]];    
  112. }
  113.  
  114. - manual:sender
  115. {
  116. DEBUG_FUNC1(DEBUGLEVEL);
  117.     return [helpAgent activateAgent:[sender selectedCell]];    
  118. }
  119.  
  120. - preferences:sender
  121. {
  122. DEBUG_FUNC1(DEBUGLEVEL);
  123.     return [prefAgent activateAgent:[sender selectedCell]];    
  124. }
  125.  
  126. - newDocument:sender
  127. {
  128.     id new=[DocWin new];
  129.     NXPoint p,q;
  130.  
  131. DEBUG_FUNC1(DEBUGLEVEL);
  132.     [[prefAgent getDocOrigin:&p] getDocOffset:&q];
  133.     [new moveTo:(p.x+q.x*(numDocsOpened%NEW_ROTATENUM)) :(p.y+q.y*(numDocsOpened%NEW_ROTATENUM))];
  134.     numDocsOpened++;
  135.     return [new makeKeyAndOrderFront:self];
  136. }
  137.  
  138. - openDocument:sender
  139. {
  140.     id new=[DocWin performOpenFromFile:NULL];
  141.     NXPoint p,q;
  142.  
  143. DEBUG_FUNC1(DEBUGLEVEL);
  144.     if (new==nil) return self;
  145.     [[prefAgent getDocOrigin:&p] getDocOffset:&q];
  146.     [new moveTo:(p.x+q.x*(numDocsOpened%NEW_ROTATENUM)) :(p.y+q.y*(numDocsOpened%NEW_ROTATENUM))];
  147.     numDocsOpened++;
  148.     return [new makeKeyAndOrderFront:self];
  149. }
  150.  
  151. - find:sender
  152. {
  153. DEBUG_FUNC1(DEBUGLEVEL);
  154.     return [findAgent activateAgent:[sender selectedCell]];
  155. }
  156.  
  157. - findNext:sender
  158. {
  159. DEBUG_FUNC1(DEBUGLEVEL);
  160.     return [findAgent findForward:[sender selectedCell]];
  161. }
  162.  
  163. - findPrevious:sender
  164. {
  165. DEBUG_FUNC1(DEBUGLEVEL);
  166.     return [findAgent findBackward:[sender selectedCell]];
  167. }
  168.  
  169. - evaluate:sender
  170. {
  171.     NXSelPt start,end;
  172.     char *theSelection;
  173.     id text;
  174.  
  175. DEBUG_FUNC1(DEBUGLEVEL);
  176.     text = [[NXApp mainWindow] textView];
  177.     if (text==nil) return self;
  178.     switch ([[sender selectedCell] tag])
  179.       {
  180.         case MENU_ACTIONS_Evaluate: if (*(theSelection = [text getSelection]))
  181.                       [protocolObj send:theSelection];
  182.                     else
  183.                       {
  184.                     [text getSel:&start :&end];
  185.                     if (![text selectScope])
  186.                       {
  187.                         NXBeep();
  188.                         break;
  189.                       }
  190.                     NXPing();
  191.                     [protocolObj send:(theSelection = [text getSelection])];
  192.                     if ([[NXApp mainWindow] isMemberOf:[DocWin class]])
  193.                       [text setSel:start.cp :end.cp];
  194.                       }
  195.                     [protocolObj send:"\n"];
  196.                     if ([prefAgent transcriptMode])
  197.                                       {
  198.                         [text appendNewlineIfNeeded];
  199.                                         [protocolObj sendOutput:theSelection];
  200.                       }
  201.                     break;
  202.     case MENU_ACTIONS_EvalAll: [text getSel:&start :&end];
  203.                    [protocolObj send:(theSelection = [text getAll])];
  204.                    if ([[NXApp mainWindow] isMemberOf:[DocWin class]])
  205.                      [text setSel:start.cp :end.cp];
  206.                    [protocolObj send:"\n"];
  207.                     if ([prefAgent transcriptMode])
  208.                                       {
  209.                         [text appendNewlineIfNeeded];
  210.                                         [protocolObj sendOutput:theSelection];
  211.                       }
  212.                    break;
  213.  
  214.       }
  215.     return self;
  216. }
  217.  
  218. - abort:sender
  219. {
  220. DEBUG_FUNC1(DEBUGLEVEL);
  221.     switch ([[sender selectedCell] tag])
  222.       {
  223.         case MENU_ACTIONS_AbortTop  : [protocolObj sendSignal:SIGNALABORTTOP]; break;
  224.     case MENU_ACTIONS_AbortSame : [protocolObj sendSignal:SIGNALABORTSAME]; break;
  225.     case MENU_ACTIONS_AbortPrev : [protocolObj sendSignal:SIGNALABORTPREV]; break;
  226.         case MENU_ACTIONS_Breakpoint: [protocolObj sendSignal:SIGNALBREAKPOINT]; break;
  227.       }
  228.     return self;
  229. }
  230.  
  231. - print:sender
  232. {
  233. DEBUG_FUNC1(DEBUGLEVEL);
  234.     return [keyWindow print:sender];
  235. }
  236.  
  237. - (id)findAgent        { return findAgent;}
  238. - (id)helpAgent        { return helpAgent;}
  239. - (id)prefAgent        { return prefAgent;}
  240. - (id)infoAgent        { return infoAgent;}
  241. - (id)interactionWindow    { return interactionWindow;}
  242. - (id)protocolObj    { return protocolObj;}
  243.  
  244. - openGraphicsWindow:(unsigned)windowNum width:(unsigned)width height:(unsigned)height
  245. {
  246.     NXRect r={{0.0,0.0},{width,height}};
  247.     id newGraphics=[[GraphicsView allocFromZone:graphicsZone] initFrame:&r window:windowNum];
  248. DEBUG_FUNC1(DEBUGLEVEL);
  249.     if (newGraphics==nil)
  250.       return nil;
  251.     [graphicsViews insertKey:(const void *)windowNum value:(void *)newGraphics];
  252.     return self;
  253. }
  254.  
  255. - closeGraphicsWindow:(unsigned)windowNum
  256. {
  257. DEBUG_FUNC1(DEBUGLEVEL);
  258.     [[(id)[graphicsViews valueForKey:(const void *)windowNum] window] setCloseEnabled];
  259.     [graphicsViews removeKey:(const void *)windowNum];
  260.     return self;
  261. }
  262.  
  263. - setGraphicsWindowFlush:(unsigned)windowNum mode:(unsigned)mode
  264. {
  265. DEBUG_FUNC1(DEBUGLEVEL);
  266.     [(id)[graphicsViews valueForKey:(const void *)windowNum] setFlushMode:mode];
  267.     return self;
  268. }
  269.  
  270. - printGraphicsWindow:(unsigned)windowNum
  271. {
  272. DEBUG_FUNC1(DEBUGLEVEL);
  273.     [(id)[graphicsViews valueForKey:(const void *)windowNum] printPSCode:self];
  274.     return self;
  275. }
  276.  
  277. @end
  278.  
  279. @implementation Main (Subclass)
  280.  
  281. - restartSubprocess
  282. {
  283.     [[protocolObj terminate:self withPrejudice:YES] free];
  284.     protocolObj = [[SchemeProtocol allocFromZone:[self zone]] init];
  285.     if (protocolObj==nil)
  286.       NXRunAlertPanel(SCHEMEPROCMESSAGE,PROCESSRESTARTBAD,NULL,NULL,NULL);
  287.     else
  288.       NXRunAlertPanel(SCHEMEPROCMESSAGE,PROCESSRESTARTOK,NULL,NULL,NULL);
  289.     return self;
  290. }
  291.  
  292. - sendEvent:(NXEvent *)theEvent
  293. {
  294.     if ((theEvent->type==NX_KEYDOWN)&&(theEvent->data.key.keyCode==73)&&(theEvent->flags&NX_NEXTCTRLKEYMASK)&&(theEvent->flags&NX_NEXTLALTKEYMASK))
  295.       [self restartSubprocess];
  296.     return [super sendEvent:theEvent];
  297. }
  298.  
  299. @end
  300.  
  301. @implementation Main (Delegate)
  302.  
  303. - (BOOL)appAcceptsAnotherFile:sender
  304. {
  305. DEBUG_FUNC1(DEBUGLEVEL);
  306.     return YES;
  307. }
  308.  
  309. - (BOOL)app:sender openFile:(const char *)file type:(const char *)aType
  310. {
  311.     id new;
  312.     NXPoint p,q;
  313.  
  314. DEBUG_FUNC1(DEBUGLEVEL);
  315.     if (!file||((new=[DocWin performOpenFromFile:(char *)file])==nil))
  316.       return NO;
  317.     [[prefAgent getDocOrigin:&p] getDocOffset:&q];
  318.     [new moveTo:(p.x+q.x*(numDocsOpened%NEW_ROTATENUM)) :(p.y+q.y*(numDocsOpened%NEW_ROTATENUM))];
  319.     numDocsOpened++;
  320.     [new makeKeyAndOrderFront:self];
  321.     return YES;
  322. }
  323.  
  324. - _saveUnsaved
  325. {
  326.     id *clist=NX_ADDRESS(windowList);
  327.     id docwins=[DocWin class];
  328.     int i;
  329.  
  330.     for(i=[windowList count]; i--;)
  331.       if ([clist[i] isDocEdited]&&[clist[i] isMemberOf:docwins])
  332.     {
  333.       switch (NXRunAlertPanel(QUITALERTTITLE, QUITALERTMESSAGE, QUITALERTDEFAULT, QUITALERTALTERNATE, QUITALERTOTHER))
  334.         {
  335.           case NX_ALERTOTHER    : return nil;
  336.           case NX_ALERTDEFAULT  : clist=NX_ADDRESS(windowList);
  337.                                       for(i=[windowList count]; i--;)
  338.                         if ([clist[i] isMemberOf:docwins]&&![[clist[i] makeKeyAndOrderFront:self] performClose:self])
  339.                       return nil;
  340.         }
  341.       break;
  342.     }
  343.     if ([interactionWindow isDocEdited]&&(i<0))
  344.       [interactionWindow makeKeyAndOrderFront:self];
  345.       switch (NXRunAlertPanel(QUITALERTTITLE, QUITALERTMESSAGE2, SAVEALERTDEFAULT, SAVEALERTALTERNATE, SAVEALERTOTHER))
  346.     {
  347.       case NX_ALERTOTHER  : return nil;
  348.       case NX_ALERTDEFAULT: [interactionWindow performSave:self];
  349.     }
  350.     return self;
  351. }
  352.  
  353. - app:sender powerOffIn:(int)ms andSave:(int)anInt
  354. {
  355.     port_t port=NXPortFromName(NX_WORKSPACEREQUEST, NULL);
  356.     int actual;
  357.  
  358.     if ((port!=PORT_NULL)&&[prefAgent confirmOnQuit])
  359.       {
  360.         [appSpeaker setSendPort:port];
  361.     if (![appSpeaker extendPowerOffBy:600000 actual:&actual])
  362.           {
  363.         BOOL confirm = [prefAgent confirmOnClose];
  364.         terminus = YES;
  365.         if (!confirm)
  366.           [prefAgent setConfirmOnClose:YES];
  367.         [self _saveUnsaved];
  368.         if (!confirm)
  369.           [prefAgent setConfirmOnClose:NO];
  370.       }
  371.       }
  372.     [protocolObj terminate:self withPrejudice:YES];
  373.     return self;
  374. }
  375.  
  376. - appWillTerminate:sender
  377. {
  378.     BOOL confirm = [prefAgent confirmOnClose];
  379.  
  380.     if (!confirm)
  381.       [prefAgent setConfirmOnClose:YES];
  382.     if ([prefAgent confirmOnQuit]&&([self _saveUnsaved]==nil))
  383.       {
  384.         if (!confirm)
  385.           [prefAgent setConfirmOnClose:NO];
  386.         return nil;
  387.       }
  388.     terminus = YES;
  389.     [protocolObj terminate:self withPrejudice:YES];
  390.     return self;
  391. }
  392.  
  393. @end
  394.  
  395. @implementation MyFontPanel
  396.  
  397. + newContent:(const NXRect *)contentRect style:(int)aStyle backing:(int)bufferingType buttonMask:(int)mask defer:(BOOL)flag
  398. {
  399.     int i;
  400.     
  401. DEBUG_FUNC1(DEBUGLEVEL);
  402.     self = [super newContent:contentRect style:aStyle backing:bufferingType buttonMask:mask defer:flag];
  403.     for(i=([families cellCount]-1); i>=0; i--)
  404.       if (NXOrderStrings((unsigned char *)[[families cellAt:i :0] stringValue], (unsigned char *)GOODFONT1, YES, -1, NULL)
  405.           &&NXOrderStrings((unsigned char *)[[families cellAt:i :0] stringValue], (unsigned char *)GOODFONT2, YES, -1, NULL))
  406.         [families removeRowAt:i andFree:YES];
  407.     [families sizeToCells];
  408.     selFont = [[[NXApp mainWindow] textView] font];
  409.     selMetrics = [selFont metrics];
  410.     return self;
  411. }
  412.  
  413. @end
  414.  
  415. @implementation Window (PrintingExt)
  416.  
  417. - print:sender
  418. {
  419.     id keyWindow = [NXApp keyWindow];
  420.     if ([[keyWindow contentView] hasSubviewOfKind:[GraphicsView class]])
  421.       [[[keyWindow contentView] docView] printPSCode:self];
  422.     else if ([[keyWindow contentView] hasSubviewOfKind:[ScrollView class]])
  423.       {
  424.         if (!NXOrderStrings((unsigned char *)[keyWindow title],(unsigned char *)LICENSEPANELTITLE,YES,-1,NULL))
  425.       [[[[[keyWindow contentView] subviews] objectAt:0] docView] printPSCode:self];
  426.     else if (!NXOrderStrings((unsigned char *)[keyWindow title],(unsigned char *)HELPPANELTITLE,YES,-1,NULL))
  427.       [[[[keyWindow delegate] helpScrollView] docView] printPSCode:self];
  428.       }
  429.     return self;
  430. }
  431.  
  432. @end
  433.  
  434. @implementation View (PrintingExt)
  435.  
  436. - (BOOL)hasSubviewOfKind:aClass
  437. {
  438.     int i;
  439.     if ([self isKindOf:aClass])
  440.       return YES;
  441.     else if ([self subviews]==nil)
  442.       return NO;
  443.     else
  444.       for(i=0; i<[subviews count]; i++)
  445.         if ([[subviews objectAt:i] hasSubviewOfKind:aClass])
  446.       return YES;
  447.     return NO;
  448. }
  449.  
  450. @end
  451.