home *** CD-ROM | disk | FTP | other *** search
- /****
- * CATalkDoc.c
- *
- * Document methods for a adsp application.
- *
- * I didnt do much to change this from the starter doc. It was a quick hack.
- * I preferred to spend my time making the ADSP, NBP and Messenger look good.
- *
- * Copyright © 1992 Bernie Bernstein. All rights reserved.
- *
- ****/
-
- #include "Global.h"
- #include "Commands.h"
- #include "CApplication.h"
- #include "CBartender.h"
- #include "CDataFile.h"
- #include "CDecorator.h"
- #include "CDesktop.h"
- #include "CError.h"
- #include "CPanorama.h"
- #include "CScrollPane.h"
- #include "CATalkDoc.h"
- #include "CATalkPane.h"
- #include "TBUtilities.h"
- #include "CWindow.h"
- #include <Packages.h>
- #include <CDLOGDirector.h>
- #include <CArray.h>
- #include <Constants.h>
-
- #include "ATalk.h"
- #include "CMessenger.h"
- #include "CNBP.h"
- #include "CNBPPane.h"
- #include "CADSPInPane.h"
- #include "CADSPOutPane.h"
-
- #define WINDATalk 500 /* Resource ID for WIND template */
-
- extern CApplication *gApplication; /* The application */
- extern CBartender *gBartender; /* The menu handling object */
- extern CDecorator *gDecorator; /* Window dressing object */
- extern CDesktop *gDesktop; /* The enclosure for all windows */
- extern CBureaucrat *gGopher; /* The current boss in the chain of command */
- extern OSType gSignature; /* The application's signature */
- extern CError *gError; /* The global error handler */
-
- /***
- * IATalkDoc
- *
- * This is your document's initialization method.
- * If your document has its own instance variables, initialize
- * them here.
- *
- * The least you need to do is invoke the default method.
- *
- ***/
-
-
- void CATalkDoc::IATalkDoc(CApplication *aSupervisor, Boolean printable)
-
- {
- CDocument::IDocument(aSupervisor, printable);
-
- StartTalker();
- }
-
-
- /***
- * Dispose
- *
- * This is your document's destruction method.
- * If you allocated memory in your initialization method
- * or opened temporary files, this is the place to release them.
- *
- * Be sure to call the default method!
- *
- ***/
-
- void CATalkDoc::Dispose()
-
- {
- DoKillConnection();
- inherited::Dispose();
- }
-
-
- /***
- * DoCommand
- *
- * This is the heart of your document.
- * In this method, you handle all the commands your document
- * deals with.
- *
- * Be sure to call the default method to handle the standard
- * document commands: cmdClose, cmdSave, cmdSaveAs, cmdRevert,
- * cmdPageSetup, cmdPrint, and cmdUndo. To change the way these
- * commands are handled, override the appropriate methods instead
- * of handling them here.
- *
- ***/
-
- void CATalkDoc::DoCommand(long theCommand)
-
- {
- switch (theCommand) {
-
- /* your document commands here */
- case cmdOpenConnection:
- DoConnect();
- break;
-
- case cmdCloseConnection:
- DoCloseConnection();
- break;
-
- default: inherited::DoCommand(theCommand);
- break;
- }
- }
-
-
- /***
- * UpdateMenus
- *
- * In this method you can enable menu commands that apply when
- * your document is active.
- *
- * Be sure to call the inherited method to get the default behavior.
- * The inherited method enables these commands: cmdClose, cmdSaveAs,
- * cmdSave, cmdRevert, cmdPageSetup, cmdPrint, cmdUndo.
- *
- ***/
-
- void CATalkDoc::UpdateMenus()
-
- {
- inherited::UpdateMenus();
-
- /* Enable your menu commands here (enable each one with a call to
- gBartender->EnableCmd(command_number)).
- */
-
- gBartender->EnableCmd(cmdOpenConnection);
- gBartender->EnableCmd(cmdCloseConnection);
- }
-
-
- /***
- * NewFile
- *
- * When the user chooses New from the File menu, the CreateDocument()
- * method in your Application class will send a newly created document
- * this message. This method needs to create a new window, ready to
- * work on a new document.
- *
- * Since this method and the OpenFile() method share the code for creating
- * the window, you should use an auxiliary window-building method.
- *
- ***/
- void CATalkDoc::NewFile(void)
-
- {
- Str255 wTitle; /* Window title string. */
- short wCount; /* Index number of new window. */
- Str255 wNumber; /* Index number as a string. */
-
- BuildWindow(NULL);
-
- itsWindow->GetTitle(wTitle);
- wCount = gDecorator->GetWCount();
- NumToString(wCount, wNumber);
- ConcatPStrings(wTitle, (StringPtr) "\p ");
- ConcatPStrings(wTitle, wNumber);
- itsWindow->SetTitle(wTitle);
-
- itsWindow->Select();
- }
-
-
- void CATalkDoc::OpenFile(SFReply *macSFReply)
- {
- }
-
-
-
- /***
- * BuildWindow
- *
- * This is the auxiliary window-building method that the
- * NewFile() and OpenFile() methods use to create a window.
- *
- * In this implementation, the argument is the data to display.
- *
- ***/
-
- void CATalkDoc::BuildWindow (Handle theData)
-
- {
- CScrollPane *theScrollPane;
- CATalkPane *theMainPane;
-
- itsWindow = new(CWindow);
- itsWindow->IWindow(WINDATalk, FALSE, gDesktop, this);
-
- theScrollPane = new(CScrollPane);
-
- theScrollPane->IScrollPane(itsWindow, this, 10, 10, 0, 0,
- sizELASTIC, sizELASTIC,
- FALSE, FALSE, TRUE);
-
- theScrollPane->FitToEnclFrame(TRUE, TRUE);
-
- theMainPane = new(CATalkPane);
- theMainPane->IATalkPane(theScrollPane, this, 10, 10, 0, 0, sizELASTIC, sizELASTIC);
- theMainPane->SetScales(1,1);
- itsMainPane = theMainPane;
- itsGopher = theMainPane;
-
- theMainPane->FitToEnclosure(TRUE, TRUE);
-
- theScrollPane->InstallPanorama(theMainPane);
-
- gDecorator->PlaceNewWindow(itsWindow);
-
- BuildSubPanes();
- }
-
-
- /***
- * BuildSubPanes
- *
- * Make an input and output pane
- ***/
- void CATalkDoc::BuildSubPanes(void)
- {
- CScrollPane *inScrollPane;
- CScrollPane *outScrollPane;
- CADSPInPane *inEditor;
- CADSPOutPane *outEditor;
- LongRect frame;
- short width, height;
- short spacing = 5;
- short subheight;
- StringHandle user;
-
- itsMainPane->GetLengths(&width, &height);
- itsMainPane->GetFrame(&frame);
-
- height -= SBARSIZE;
- subheight = height/2 - spacing;
-
- inScrollPane = new(CScrollPane);
- inScrollPane->IScrollPane(itsMainPane, this, width, subheight, 0, 0,
- sizELASTIC, sizFIXEDSTICKY,
- FALSE, TRUE, FALSE);
- inEditor = new(CADSPInPane);
- inEditor->IADSPInPane(inScrollPane, this, itsMessenger);
- inEditor->Specify(FALSE, TRUE, FALSE);
- inEditor->FitToEnclosure(TRUE, TRUE);
- inScrollPane->InstallPanorama(inEditor);
- itsReceiver = inEditor;
-
- outScrollPane = new(CScrollPane);
- outScrollPane->IScrollPane(itsMainPane, this, width, subheight, 0, frame.bottom-subheight-SBARSIZE,
- sizELASTIC, sizFIXEDSTICKY,
- FALSE, TRUE, FALSE);
- outEditor = new(CADSPOutPane);
- outEditor->IADSPOutPane(outScrollPane, this, itsMessenger);
- outEditor->Specify(TRUE, TRUE, TRUE);
- outEditor->SetWantsClicks(TRUE);
- outEditor->FitToEnclosure(TRUE, TRUE);
- outScrollPane->InstallPanorama(outEditor);
- itsGopher = outEditor;
-
- ((CATalkPane*)itsMainPane)->SetSubPanes(inScrollPane, outScrollPane);
- }
-
-
-
- Boolean CATalkDoc::DoSave(void)
- {
- }
-
-
- Boolean CATalkDoc::DoSaveAs(SFReply *macSFReply)
- {
- }
-
-
- void CATalkDoc::DoRevert(void)
- {
- }
-
-
-
-
-
-
- /**********************************************************************
-
- Interesting stuff (ADSP)
-
- **********************************************************************/
-
-
-
-
-
-
- /***
- * StartTalker
- *
- * Post the NBP name for this host
- ***/
- void CATalkDoc::StartTalker(void)
- {
- StringHandle user;
- long ticks = TickCount();
- Str32 tickstr;
- Str32 object;
- CMessenger *aMessenger;
-
- NumToString(ticks, tickstr);
- user = GetString(-16096); // chooser name
- CopyPString(*user, object);
- ConcatPStrings(object, tickstr);
-
- aMessenger = new(CMessenger);
- aMessenger->IMessenger();
- aMessenger->SetName(object, "\pADSP Demo", "\p*");
- aMessenger->MakeConnector();
- aMessenger->WaitForSession();
- itsMessenger = aMessenger;
- }
-
-
- /***
- * DoConnect
- *
- * Find another talker and open a connection to it.
- ***/
- void CATalkDoc::DoConnect(void)
- {
- CArray *matches = nil;
- CDLOGDirector *dlog = nil;
- long cmd;
- CScrollPane *listPane;
- CNBPPane *listTable;
- Cell aCell;
- CNBP *anNBP;
- Boolean oldSelf;
-
- TRY
- {
- matches = new CArray;
- matches->IArray(sizeof(NBPAddrNameRec));
-
- // find nbp names of other talkers
- anNBP = itsMessenger->GetNBP();
-
- anNBP->MatchNames("\p=", "\pADSP Demo", "\p*", matches);
-
- // ask the user to pick someone to talk to
- if (matches->GetNumItems() > 0)
- {
- dlog = new CDLOGDirector;
- dlog->IDLOGDirector( DLOGChooseNBP, gApplication);
- listPane = (CScrollPane*)dlog->FindViewByID( kListItem);
- ASSERT(listPane);
- listTable = new CNBPPane;
- listTable->INBPPane(listPane, dlog->itsWindow, 0, 0, 0, 0, sizELASTIC, sizELASTIC);
- listTable->FitToEnclosure( TRUE, TRUE);
- listPane->InstallPanorama( listTable);
- listTable->SetArray( matches, FALSE);
- dlog->BeginDialog();
- cmd = dlog->DoModalDialog(cmdOK);
- if (cmd == cmdOK)
- {
- AddrBlock addr = listTable->GetSelectedAddr();
- // open the session
- itsMessenger->OpenSession(addr);
- }
- }
- ForgetObject(dlog);
- ForgetObject(matches);
- }
- CATCH
- {
- ForgetObject(dlog);
- ForgetObject(matches);
- }
- ENDTRY;
- }
-
-
- /***
- * DoCloseConnection
- *
- * Close the current connection
- ***/
- void CATalkDoc::DoCloseConnection(void)
- {
- if (itsMessenger)
- itsMessenger->CloseSession();
- }
-
-
- /***
- * DoKillConnection
- *
- * Remove the connection
- ***/
- void CATalkDoc::DoKillConnection(void)
- {
- if (itsMessenger)
- itsMessenger->KillConnector();
- }
-
-
-
- /***
- * Dawdle
- *
- * If there is some new input, then we should put it into the pane
- ***/
- void CATalkDoc::Dawdle(long *maxSleep)
- {
- if (itsMessenger)
- {
- *maxSleep = 30;
- if (!itsMessenger->StillWaitingForSession())
- {
- if (itsMessenger->MessageWaiting())
- {
- itsReceiver->GetData();
- }
- }
- }
- inherited::Dawdle(maxSleep);
- }
-
-