例 B-2 に示す CoEd.C ファイルは、各アプリケーションに組み込む必要がある ToolTalk コードを示します。これにより、ツールキットの初期化、ToolTalk セッションへの参加、パターンの登録、ToolTalk サービスのイベント・ループへの追加を実行できます。
このファイルには、エディタ・アプリケーションとして機能する場合の CoEd に固有の ToolTalk コードも入っています。このコードには、ptype の宣言と起動メッセージの処理も含まれています。
/* * CoEd.cc * * Copyright (c) 1991,1993 by Sun Microsystems. */ #include <stdlib.h> #include <desktop/tttk.h> // Include the ToolTalk messaging toolkit #include <CoEd.h> #include "CoEditor.h" #include "CoEdTextBuffer.h" XtAppContext myContext; Widget myTopWidget = 0; Display *myDpy; int abortCode = 0; Tt_pattern *sessPats = 0; // Patterns returned when session joined int timeOutFactor = 1000; int maxBuffers = 1000; int *pArgc; char **globalArgv; const char *ToolName = "CoEd"; const char *usage = "Usage: CoEd [-p01] [-w n] [-t n] [file]¥n" " -p print ToolTalk procid¥n" " -0 do not open an initial composition window¥n" " -1 be a single-buffer editor¥n" " -w sleep for n seconds before coming up¥n" " -t use n as timeout factor, in milliseconds (default: 1000)¥n" ; void main( int argc, char **argv ) { static const char *here = "main()"; int delay = 0; int printid = 0; int compose = 1; char *file = 0; OlToolkitInitialize( 0 ); XtToolkitInitialize(); myContext = XtCreateApplicationContext(); // // This display may get closed, and another opened, inside // CoEditor::_init(), if e.g. our parent is on a different screen // pArgc = &argc; globalArgv = argv; myDpy = XtOpenDisplay( myContext, 0, 0, "CoEd", 0, 0, &argc, argv ); int c; while ((c = getopt( argc, argv, "p01w:t:" )) != -1) { switch (c) { case `p': printid = 1; break; case `0': compose = 0; break; case `1': maxBuffers = 1; break; case `w': delay = atoi( optarg ); break; case `t': timeOutFactor = atoi( optarg ); break; default: fputs( usage, stderr ); exit( 1 ); } } if (optind < argc) { file = argv[ optind ]; } while (delay > 0) { sleep( 1 ); delay--; } int myTtFd; // Obtain process identifier // Initialize toolkit and create a ToolTalk communication endpoint char *myProcID = ttdt_open( &myTtFd, ToolName, "SunSoft", "%I", 1 ); // Declare ptype ttmedia_ptype_declare( "DT_CoEd", 0, CoEditor::loadISOLatin1_, (void *)&myTopWidget, 1 ); // Process the message that started us, if any tttk_Xt_input_handler( 0, 0, 0 ); if (abortCode != 0) { // Error in message that caused us to start. exit( abortCode ); } if (CoEditor::numEditors == 0) { // started by hand, not by ToolTalk if (file == 0) { if (compose) { new CoEditor( &myTopWidget ); } } else { new CoEditor( &myTopWidget, file ); } } // // If sessPats is unset, then we have not joined the desktop // session yet. So join it. // if (sessPats == 0) { Widget session_shell = CoEditor::editors[0]->shell; if (maxBuffers > 1) { // // In multi-window mode, no single window is the // distinguished window. // session_shell = myTopWidget; } sessPats = ttdt_session_join( 0, 0, session_shell, 0, 1 ); } XtAppAddInput( myContext, myTtFd,(XtPointer)XtInputReadMask, tttk_Xt_input_handler, myProcID ); XtAppMainLoop( myContext ); }