Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / tt / demo / CoEd / CoEd / CoEd.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 //%%  (c) Copyright 1993, 1994 Hewlett-Packard Company                  
24 //%%  (c) Copyright 1993, 1994 International Business Machines Corp.    
25 //%%  (c) Copyright 1993, 1994 Sun Microsystems, Inc.                   
26 //%%  (c) Copyright 1993, 1994 Novell, Inc.                             
27 //%%  $XConsortium: CoEd.C /main/3 1995/10/20 17:04:04 rswiston $                                                       
28 /*
29  * CoEd.cc
30  *
31  * Copyright (c) 1991,1993 by Sun Microsystems.
32  */
33
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <Tt/tttk.h>
37 #include <CoEd.h>
38 #include "CoEditor.h"
39 #include "CoEdTextBuffer.h"
40
41 XtAppContext    myContext;
42 Widget          myTopWidget     = 0;
43 Display        *myDpy;
44 char           *myAltMediaType  = "ISO_Latin_1";
45 int             abortCode       = 0;
46 Tt_pattern     *sessPats        = 0;
47 int             timeOutFactor   = 1000;
48 int             maxBuffers      = 1000;
49 int            *pArgc;
50 char          **globalArgv;
51
52 const char     *ToolName        = "CoEd";
53 const char     *usage           =
54 "Usage: CoEd [-p01] [-w n] [-t n] [-m media_type] [file]\n"
55 "       -p      print ToolTalk procid\n"
56 "       -0      do not open an initial composition window\n"
57 "       -1      be a single-buffer editor\n"
58 "       -w      sleep for n seconds before coming up\n"
59 "       -t      use n as timeout factor, in milliseconds (default: 1000)\n"
60 "       -m      set the alternate media type (default: ISO_Latin_1)\n"
61 ;
62
63 void
64 main(
65         int    argc,
66         char **argv
67 )
68 {
69         static const char *here = "main()";
70         int   delay   = 0;
71         int   printid = 0;
72         int   daemon  = 0;
73         char *file    = 0;
74
75         XtToolkitInitialize();
76         myContext = XtCreateApplicationContext();
77         //
78         // This display may get closed, and another opened, inside
79         // CoEditor::_init(), if e.g. our parent is on a different screen
80         //
81         pArgc = &argc;
82         globalArgv = argv;
83         myDpy = XtOpenDisplay( myContext, 0, 0, "CoEd", 0, 0, &argc, argv );
84
85         int c;
86         while ((c = getopt( argc, argv, "p01w:t:m:" )) != -1) {
87                 switch (c) {
88                     case 'p':
89                         printid = 1;
90                         break;
91                     case '0':
92                         daemon = 1;
93                         break;
94                     case '1':
95                         maxBuffers = 1;
96                         break;
97                     case 'w':
98                         delay = atoi( optarg );
99                         break;
100                     case 't':
101                         timeOutFactor = atoi( optarg );
102                         break;
103                     case 'm':
104                         myAltMediaType = optarg;
105                         break;
106                     default:
107                         fputs( usage, stderr );
108                         exit( 1 );
109                 }
110         }
111         if (optind < argc) {
112                 file = argv[ optind ];
113         }
114         while (delay > 0) {
115                 sleep( 1 );
116                 delay--;
117         }
118
119         int myTtFd;
120         char *myProcID = ttdt_open( &myTtFd, ToolName, "SunSoft", "%I", 1 );
121         Tt_status status = tt_ptr_error( myProcID );
122         if (status != TT_OK) {
123                 fprintf( stderr, "ttdt_open(): %s\n",
124                          tt_status_message(status) );
125                 myProcID = 0;
126         } else {
127                 if (printid) {
128                         printf( "%s\n", myProcID );
129                 }
130
131                 XtAppAddInput( myContext, myTtFd, (XtPointer)XtInputReadMask,
132                                tttk_Xt_input_handler, myProcID );
133
134                 ttmedia_ptype_declare( "Sun_CoEd", 0, CoEditor::loadISOLatin1_,
135                                        (void *)&myTopWidget, 1 );
136
137                 // Process the message that started us, if any
138                 tttk_Xt_input_handler( 0, 0, 0 );
139                 if (abortCode != 0) {
140                         // Error in message that caused us to start.
141                         exit( abortCode );
142                 }
143         }
144
145         if (CoEditor::numEditors == 0) {
146                 // started by hand, not by ToolTalk
147                 if (file == 0) {
148                         if (! daemon) {
149                                 new CoEditor( &myTopWidget );
150                         }
151                 } else {
152                         new CoEditor( &myTopWidget, file );
153                 }
154         }
155         //
156         // If sessPats is unset, then we have no joined the desktop
157         // session yet.  So join it.
158         //
159         if (sessPats == 0) {
160                 Widget session_shell = CoEditor::editors[0]->shell;
161                 if (maxBuffers > 1) {
162                         //
163                         // In multi-window mode, no single window is the
164                         // distinguished window.
165                         //
166                         session_shell = myTopWidget;
167                 }
168                 sessPats = ttdt_session_join( 0, CoEditor::contractCB_,
169                                 session_shell, CoEditor::editors[0], 1 );
170         }
171
172         while (daemon || (CoEditor::numEditors > 0)) {
173                 XEvent event;
174                 XtAppNextEvent( myContext, &event );
175                 XtDispatchEvent( &event );
176         }
177         ttdt_close( 0, 0, 1 );
178 }