home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / CADSP 1.0 / Demo / CATalkDoc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-30  |  9.3 KB  |  444 lines  |  [TEXT/KAHL]

  1. /****
  2.  * CATalkDoc.c
  3.  *
  4.  *    Document methods for a adsp application.
  5.  *
  6.  *        I didnt do much to change this from the starter doc. It was a quick hack.
  7.  *        I preferred to spend my time making the ADSP, NBP and Messenger look good.
  8.  *
  9.  *  Copyright © 1992 Bernie Bernstein.  All rights reserved.
  10.  *
  11.  ****/
  12.  
  13. #include "Global.h"
  14. #include "Commands.h"
  15. #include "CApplication.h"
  16. #include "CBartender.h"
  17. #include "CDataFile.h"
  18. #include "CDecorator.h"
  19. #include "CDesktop.h"
  20. #include "CError.h"
  21. #include "CPanorama.h"
  22. #include "CScrollPane.h"
  23. #include "CATalkDoc.h"
  24. #include "CATalkPane.h"
  25. #include "TBUtilities.h"
  26. #include "CWindow.h"
  27. #include <Packages.h>
  28. #include <CDLOGDirector.h>
  29. #include <CArray.h>
  30. #include <Constants.h>
  31.  
  32. #include "ATalk.h"
  33. #include "CMessenger.h"
  34. #include "CNBP.h"
  35. #include "CNBPPane.h"
  36. #include "CADSPInPane.h"
  37. #include "CADSPOutPane.h"
  38.  
  39. #define    WINDATalk        500        /* Resource ID for WIND template */
  40.  
  41. extern    CApplication *gApplication;    /* The application */
  42. extern    CBartender    *gBartender;    /* The menu handling object */
  43. extern    CDecorator    *gDecorator;    /* Window dressing object    */
  44. extern    CDesktop    *gDesktop;        /* The enclosure for all windows */
  45. extern    CBureaucrat    *gGopher;        /* The current boss in the chain of command */
  46. extern    OSType        gSignature;        /* The application's signature */
  47. extern    CError        *gError;        /* The global error handler */
  48.  
  49. /***
  50.  * IATalkDoc
  51.  *
  52.  *    This is your document's initialization method.
  53.  *    If your document has its own instance variables, initialize
  54.  *    them here.
  55.  *
  56.  *    The least you need to do is invoke the default method.
  57.  *
  58.  ***/
  59.  
  60.  
  61. void CATalkDoc::IATalkDoc(CApplication *aSupervisor, Boolean printable)
  62.  
  63. {
  64.     CDocument::IDocument(aSupervisor, printable);
  65.     
  66.     StartTalker();
  67. }
  68.  
  69.  
  70. /***
  71.  * Dispose
  72.  *
  73.  *    This is your document's destruction method.
  74.  *    If you allocated memory in your initialization method
  75.  *    or opened temporary files, this is the place to release them.
  76.  *
  77.  *    Be sure to call the default method!
  78.  *
  79.  ***/
  80.  
  81. void CATalkDoc::Dispose()
  82.  
  83. {
  84.     DoKillConnection();
  85.     inherited::Dispose();
  86. }
  87.  
  88.  
  89. /***
  90.  * DoCommand
  91.  *
  92.  *    This is the heart of your document.
  93.  *    In this method, you handle all the commands your document
  94.  *    deals with.
  95.  *
  96.  *    Be sure to call the default method to handle the standard
  97.  *    document commands: cmdClose, cmdSave, cmdSaveAs, cmdRevert,
  98.  *    cmdPageSetup, cmdPrint, and cmdUndo. To change the way these
  99.  *    commands are handled, override the appropriate methods instead
  100.  *    of handling them here.
  101.  *
  102.  ***/
  103.  
  104. void CATalkDoc::DoCommand(long theCommand)
  105.  
  106. {
  107.     switch (theCommand) {
  108.  
  109.         /* your document commands here */
  110.         case cmdOpenConnection:
  111.                     DoConnect();
  112.                     break;
  113.         
  114.         case cmdCloseConnection:
  115.                     DoCloseConnection();
  116.                     break;
  117.         
  118.         default:    inherited::DoCommand(theCommand);
  119.                     break;
  120.     }
  121. }
  122.  
  123.  
  124. /***
  125.  * UpdateMenus
  126.  *
  127.  *  In this method you can enable menu commands that apply when
  128.  *  your document is active.
  129.  *
  130.  *  Be sure to call the inherited method to get the default behavior.
  131.  *  The inherited method enables these commands: cmdClose, cmdSaveAs,
  132.  *  cmdSave, cmdRevert, cmdPageSetup, cmdPrint, cmdUndo.
  133.  *
  134. ***/
  135.  
  136.  void CATalkDoc::UpdateMenus()
  137.  
  138.  {
  139.   inherited::UpdateMenus();
  140.  
  141.     /* Enable your menu commands here (enable each one with a call to   
  142.        gBartender->EnableCmd(command_number)).  
  143.     */                      
  144.  
  145.     gBartender->EnableCmd(cmdOpenConnection);
  146.     gBartender->EnableCmd(cmdCloseConnection);
  147.  }
  148.  
  149.  
  150. /***
  151.  * NewFile
  152.  *
  153.  *    When the user chooses New from the File menu, the CreateDocument()
  154.  *    method in your Application class will send a newly created document
  155.  *    this message. This method needs to create a new window, ready to
  156.  *    work on a new document.
  157.  *
  158.  *    Since this method and the OpenFile() method share the code for creating
  159.  *    the window, you should use an auxiliary window-building method.
  160.  *
  161.  ***/
  162. void CATalkDoc::NewFile(void)
  163.  
  164. {    
  165.     Str255  wTitle;     /* Window title string.         */
  166.     short   wCount;     /* Index number of new window.  */
  167.     Str255  wNumber;    /* Index number as a string.    */
  168.  
  169.     BuildWindow(NULL);
  170.  
  171.     itsWindow->GetTitle(wTitle);
  172.     wCount = gDecorator->GetWCount();
  173.     NumToString(wCount, wNumber);
  174.     ConcatPStrings(wTitle, (StringPtr) "\p ");
  175.     ConcatPStrings(wTitle, wNumber);
  176.     itsWindow->SetTitle(wTitle);
  177.     
  178.     itsWindow->Select();
  179. }
  180.  
  181.  
  182. void CATalkDoc::OpenFile(SFReply *macSFReply)
  183. {
  184. }
  185.  
  186.  
  187.  
  188. /***
  189.  * BuildWindow
  190.  *
  191.  *    This is the auxiliary window-building method that the
  192.  *    NewFile() and OpenFile() methods use to create a window.
  193.  *
  194.  *    In this implementation, the argument is the data to display.
  195.  *
  196.  ***/
  197.  
  198. void CATalkDoc::BuildWindow (Handle theData)
  199.  
  200. {
  201.     CScrollPane        *theScrollPane;
  202.     CATalkPane    *theMainPane;
  203.  
  204.     itsWindow = new(CWindow);
  205.     itsWindow->IWindow(WINDATalk, FALSE, gDesktop, this);
  206.  
  207.     theScrollPane = new(CScrollPane);
  208.  
  209.     theScrollPane->IScrollPane(itsWindow, this, 10, 10, 0, 0,
  210.                                 sizELASTIC, sizELASTIC,
  211.                                 FALSE, FALSE, TRUE);
  212.  
  213.     theScrollPane->FitToEnclFrame(TRUE, TRUE);
  214.  
  215.     theMainPane = new(CATalkPane);
  216.     theMainPane->IATalkPane(theScrollPane, this, 10, 10, 0, 0, sizELASTIC, sizELASTIC);
  217.     theMainPane->SetScales(1,1);
  218.     itsMainPane = theMainPane;
  219.     itsGopher = theMainPane;
  220.  
  221.     theMainPane->FitToEnclosure(TRUE, TRUE);
  222.  
  223.     theScrollPane->InstallPanorama(theMainPane);
  224.          
  225.     gDecorator->PlaceNewWindow(itsWindow);
  226.  
  227.     BuildSubPanes();
  228. }
  229.  
  230.  
  231. /***
  232.  * BuildSubPanes
  233.  *
  234.  *        Make an input and output pane
  235.  ***/
  236. void CATalkDoc::BuildSubPanes(void)
  237. {
  238.     CScrollPane *inScrollPane;
  239.     CScrollPane *outScrollPane;
  240.     CADSPInPane *inEditor;
  241.     CADSPOutPane *outEditor;
  242.     LongRect frame;
  243.     short width, height;
  244.     short spacing = 5;
  245.     short subheight;
  246.     StringHandle user;
  247.     
  248.     itsMainPane->GetLengths(&width, &height);
  249.     itsMainPane->GetFrame(&frame);
  250.     
  251.     height -= SBARSIZE;
  252.     subheight = height/2 - spacing;
  253.     
  254.     inScrollPane = new(CScrollPane);
  255.     inScrollPane->IScrollPane(itsMainPane, this, width, subheight, 0, 0,
  256.                                 sizELASTIC, sizFIXEDSTICKY,
  257.                                 FALSE, TRUE, FALSE);
  258.     inEditor = new(CADSPInPane);
  259.     inEditor->IADSPInPane(inScrollPane, this, itsMessenger);
  260.     inEditor->Specify(FALSE, TRUE, FALSE);
  261.     inEditor->FitToEnclosure(TRUE, TRUE);
  262.     inScrollPane->InstallPanorama(inEditor);
  263.     itsReceiver = inEditor;
  264.             
  265.     outScrollPane = new(CScrollPane);
  266.     outScrollPane->IScrollPane(itsMainPane, this, width, subheight, 0, frame.bottom-subheight-SBARSIZE,
  267.                                 sizELASTIC, sizFIXEDSTICKY,
  268.                                 FALSE, TRUE, FALSE);
  269.     outEditor = new(CADSPOutPane);
  270.     outEditor->IADSPOutPane(outScrollPane, this, itsMessenger);
  271.     outEditor->Specify(TRUE, TRUE, TRUE);
  272.     outEditor->SetWantsClicks(TRUE);
  273.     outEditor->FitToEnclosure(TRUE, TRUE);
  274.     outScrollPane->InstallPanorama(outEditor);
  275.     itsGopher = outEditor;
  276.     
  277.     ((CATalkPane*)itsMainPane)->SetSubPanes(inScrollPane, outScrollPane);
  278. }
  279.  
  280.  
  281.  
  282. Boolean CATalkDoc::DoSave(void)
  283. {
  284. }
  285.  
  286.  
  287. Boolean CATalkDoc::DoSaveAs(SFReply *macSFReply)
  288. {
  289. }
  290.  
  291.  
  292. void CATalkDoc::DoRevert(void)
  293. {
  294. }
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301. /**********************************************************************
  302.  
  303.                         Interesting stuff (ADSP)
  304.             
  305. **********************************************************************/
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312. /***
  313.  * StartTalker
  314.  *
  315.  *        Post the NBP name for this host
  316.  ***/
  317. void CATalkDoc::StartTalker(void)
  318. {
  319.     StringHandle user;
  320.     long ticks = TickCount();
  321.     Str32 tickstr;
  322.     Str32 object;
  323.     CMessenger *aMessenger;
  324.     
  325.     NumToString(ticks, tickstr);
  326.     user = GetString(-16096); // chooser name
  327.     CopyPString(*user, object);
  328.     ConcatPStrings(object, tickstr);
  329.     
  330.     aMessenger = new(CMessenger);
  331.     aMessenger->IMessenger();
  332.     aMessenger->SetName(object, "\pADSP Demo", "\p*");
  333.     aMessenger->MakeConnector();
  334.     aMessenger->WaitForSession();
  335.     itsMessenger = aMessenger;
  336. }
  337.  
  338.  
  339. /***
  340.  * DoConnect
  341.  *
  342.  *        Find another talker and open a connection to it.
  343.  ***/
  344. void CATalkDoc::DoConnect(void)
  345. {
  346.     CArray *matches = nil;
  347.     CDLOGDirector *dlog = nil;
  348.     long cmd;
  349.     CScrollPane *listPane;
  350.     CNBPPane *listTable;
  351.     Cell aCell;
  352.     CNBP *anNBP;
  353.     Boolean oldSelf;
  354.     
  355.     TRY
  356.         {
  357.         matches = new CArray;
  358.         matches->IArray(sizeof(NBPAddrNameRec));
  359.  
  360.         // find nbp names of other talkers
  361.         anNBP = itsMessenger->GetNBP();
  362.         
  363.         anNBP->MatchNames("\p=", "\pADSP Demo", "\p*", matches);
  364.         
  365.         // ask the user to pick someone to talk to
  366.         if (matches->GetNumItems() > 0)
  367.             {
  368.             dlog = new CDLOGDirector;
  369.             dlog->IDLOGDirector( DLOGChooseNBP, gApplication);
  370.             listPane = (CScrollPane*)dlog->FindViewByID( kListItem);
  371.             ASSERT(listPane);
  372.             listTable = new CNBPPane;
  373.             listTable->INBPPane(listPane, dlog->itsWindow, 0, 0, 0, 0, sizELASTIC, sizELASTIC);
  374.             listTable->FitToEnclosure( TRUE, TRUE);
  375.             listPane->InstallPanorama( listTable);
  376.             listTable->SetArray( matches, FALSE);
  377.             dlog->BeginDialog();
  378.             cmd = dlog->DoModalDialog(cmdOK);
  379.             if (cmd == cmdOK)
  380.                 {
  381.                 AddrBlock addr = listTable->GetSelectedAddr();
  382.                 // open the session
  383.                 itsMessenger->OpenSession(addr);
  384.                 }
  385.             }
  386.         ForgetObject(dlog);
  387.         ForgetObject(matches);
  388.         }
  389.     CATCH
  390.         {
  391.         ForgetObject(dlog);
  392.         ForgetObject(matches);
  393.         }
  394.     ENDTRY;
  395. }
  396.  
  397.  
  398. /***
  399.  * DoCloseConnection
  400.  *
  401.  *        Close the current connection
  402.  ***/
  403. void CATalkDoc::DoCloseConnection(void)
  404. {
  405.     if (itsMessenger)
  406.         itsMessenger->CloseSession();
  407. }
  408.  
  409.  
  410. /***
  411.  * DoKillConnection
  412.  *
  413.  *        Remove the connection
  414.  ***/
  415. void CATalkDoc::DoKillConnection(void)
  416. {
  417.     if (itsMessenger)
  418.         itsMessenger->KillConnector();
  419. }
  420.  
  421.  
  422.  
  423. /***
  424.  * Dawdle
  425.  *
  426.  *        If there is some new input, then we should put it into the pane
  427.  ***/
  428. void CATalkDoc::Dawdle(long *maxSleep)
  429. {
  430.     if (itsMessenger)
  431.         {
  432.         *maxSleep = 30;
  433.         if (!itsMessenger->StillWaitingForSession())
  434.             {
  435.             if (itsMessenger->MessageWaiting())
  436.                 {
  437.                 itsReceiver->GetData();
  438.                 }
  439.             }
  440.         }
  441.     inherited::Dawdle(maxSleep);
  442. }
  443.  
  444.