Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtcreate / main.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 /* $TOG: main.c /main/11 1999/09/17 17:22:06 mgreess $ */
24 /*****************************************************************************/
25 /*                                                                           */
26 /*      main.c                                                               */
27 /*                                                                           */
28 /*****************************************************************************/
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <fcntl.h>
33 #include <sys/types.h>
34 #include <locale.h>
35 #include <sys/stat.h>
36 #include <sys/param.h>
37 #include <X11/Intrinsic.h>
38 #include <X11/StringDefs.h>
39 #include <Xm/XmAll.h>
40 #include <Xm/Xm.h>
41 #include <Xm/Protocols.h>
42 #include <Xm/VendorSEP.h>
43 #include <Dt/Icon.h>
44 #include <Dt/EnvControlP.h>
45 #include <Dt/UserMsg.h>
46
47 #define GETXMSTRING(s, m, d)    XmStringCreateLocalized(GETMESSAGE(s,m,d))
48 #define CLASS_NAME              "Dtcreate"
49
50 /* Copied from Xm/BaseClassI.h */
51 extern XmWidgetExtData _XmGetWidgetExtData( 
52                         Widget widget,
53 #if NeedWidePrototypes
54                         unsigned int extType) ;
55 #else
56                         unsigned char extType) ;
57 #endif /* NeedWidePrototypes */
58
59 #define NOEXTERN
60 #include "dtcreate.h"
61 #include "ca_aux.h"
62 #include "cmnrtns.h"
63
64 #ifdef __TOOLTALK
65 #include <Tt/tttk.h>
66 void DieFromToolTalkError(Widget, char*, Tt_status);
67 Tt_message ProcessToolTalkMessage( );
68 #endif
69
70 #if !defined(NL_CAT_LOCALE)
71 #define NL_CAT_LOCALE 0
72 #endif
73
74
75 /*----------------------------------------------------
76  * UxXt.h needs to be included only when compiling a
77  * stand-alone application.
78  *---------------------------------------------------*/
79 #ifndef DESIGN_TIME
80 #include "UxXt.h"
81 #endif /* DESIGN_TIME */
82
83 XtAppContext    UxAppContext;
84 Widget          UxTopLevel;
85 Display         *UxDisplay;
86 int             UxScreen;
87
88 /*----------------------------------------------
89  * Insert application global declarations here
90  *---------------------------------------------*/
91
92 extern Dimension wintypeheight;
93
94 /*  Structure used on a save session to see if a dt is iconic  */
95 typedef struct {
96    int state;
97    Window icon;
98 } WM_STATE;
99
100 /*****************************************************************************/
101 /*                                                                           */
102 /* Routine:   GetSessionInfo                                                 */
103 /*                                                                           */
104 /* Purpose:   get session management saved information                       */
105 /*                                                                           */
106 /*****************************************************************************/
107 void GetSessionInfo( void )
108 {
109     XrmDatabase        db;
110     char               *tmpStr, *tmpStr2;
111     char               *path;
112     XrmName            xrm_name[5];
113     XrmRepresentation  rep_type;
114     XrmValue           value;
115     char               *msgPtr;
116     char               *fmtPtr;
117     char               *errPtr;
118
119 #ifdef DEBUG
120   if (debug)
121     stat_out("GetSessionInfo\n");
122 #endif
123
124     /*****************************************/
125     /***  Open the resource database file  ***/
126     /*****************************************/
127
128     /************************************************************************/
129     /*  UxTopLevel is used because CreateActionAppShell isn't created yet...*/
130     /*  okay because it only uses it to get a display, not a window         */
131     /************************************************************************/
132     if (DtSessionRestorePath(UxTopLevel, &path, sessiondata.pszSessionFile) == False)
133         path = sessiondata.pszSessionFile;
134 #ifdef DEBUG
135     printf("session file = %s\n", path);
136 #endif
137     if ((db = XrmGetFileDatabase (path)) == NULL)
138     {
139        msgPtr = GETMESSAGE(5, 162, "Could not restore session from file:");
140        fmtPtr = "%s\n %s\n";
141        errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
142                           strlen(path) + 1) * sizeof(char));
143        sprintf(errPtr, fmtPtr, msgPtr, path);
144        display_error_message(UxTopLevel, errPtr);
145        XtFree(errPtr);
146        if (path != sessiondata.pszSessionFile) {
147           XtFree(path);
148        }
149        sessiondata.useSession = False;
150        return;
151     }
152     if (path != sessiondata.pszSessionFile) {
153        XtFree(path);
154     }
155
156     /*** now get the information we want from the database ***/
157     /*** make sure values are at least somewhat reasonable ***/
158
159     xrm_name[1] = NULL;
160
161     /*******************************/
162     /* get x position              */
163     /*******************************/
164     xrm_name[0] = XrmStringToQuark ("x");
165     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value))
166         sessiondata.x = (Position)atoi((char *)value.addr);
167     else
168         sessiondata.x = 100;
169     if (sessiondata.x < 0) sessiondata.x = 0;
170
171     /*******************************/
172     /* get y position              */
173     /*******************************/
174     xrm_name[0] = XrmStringToQuark ("y");
175     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value))
176         sessiondata.y = (Position)atoi((char *)value.addr);
177     else
178         sessiondata.y = 100;
179     if (sessiondata.y < 0) sessiondata.y = 0;
180
181     /*******************************/
182     /* get width                   */
183     /*******************************/
184     xrm_name[0] = XrmStringToQuark ("width");
185     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value))
186         sessiondata.width = (Dimension)atoi((char *)value.addr);
187     /*
188     else
189         sessiondata.width = DEFAULT_WIDTH;
190     if (sessiondata.width < DEFAULT_WIDTH) sessiondata.width = DEFAULT_WIDTH;
191     */
192
193     /*******************************/
194     /* get height                  */
195     /*******************************/
196     xrm_name[0] = XrmStringToQuark ("height");
197     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value))
198         sessiondata.height = (Dimension)atoi((char *)value.addr);
199     /*
200     else
201         sessiondata.height = DEFAULT_HEIGHT;
202     if (sessiondata.height < DEFAULT_HEIGHT) sessiondata.height = DEFAULT_HEIGHT;
203     */
204
205     /*******************************/
206     /* get iconic state            */
207     /*******************************/
208     xrm_name[0] = XrmStringToQuark ("iconic");
209     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value))
210     {
211         if ( value.addr!=NULL && strcmp((char *)value.addr, "True")==0 )
212             sessiondata.iconicState = IconicState;
213         else
214             sessiondata.iconicState = NormalState;
215     }
216
217     /*******************************/
218     /* get expert status           */
219     /*******************************/
220     xrm_name[0] = XrmStringToQuark ("expert");
221     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value))
222     {
223         sessiondata.showExpert = ((value.addr != NULL) &&
224                                   (strcmp((char *)value.addr, "True") == 0));
225     }
226
227     /*******************************/
228     /* get workspace #             */
229     /*******************************/
230     xrm_name[0] = XrmStringToQuark ("workspace");
231     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value))
232     {
233         if ( value.addr!=NULL)
234             strcpy(sessiondata.workspaces, value.addr);
235         else
236             sessiondata.workspaces[0] = '\0';
237     }
238
239     /*******************************/
240     /* get file name               */
241     /*******************************/
242     xrm_name[0] = XrmStringToQuark ("file");
243     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value)) {
244 #ifdef DEBUG
245        printf("file to edit = '%s'\n", value.addr);
246 #endif
247        pszFileToEdit = (char *)XtMalloc(strlen(value.addr) + 1);
248        strcpy(pszFileToEdit, value.addr);
249     }
250 }
251 /*****************************************************************************/
252 /*                                                                           */
253 /* Routine:   SaveSession                                                    */
254 /*                                                                           */
255 /* Purpose:   save state information for session management                  */
256 /*                                                                           */
257 /*****************************************************************************/
258 void SaveSession( void )
259 {
260     char                    *path, *name;
261     int                     fd, n;
262     char                    *xa_CommandStr[3];
263     char                    *tmpStr, *tmpStr2;
264     Position                x,y;
265     Dimension               width, height;
266     char                    *msgPtr, *fmtPtr, *errPtr;
267     XmVendorShellExtObject  vendorExt;
268     XmWidgetExtData         extData;
269     WM_STATE                *wmState;
270     Atom                    wmStateAtom, actualType;
271     Atom                    *ws_presence = NULL;
272     int                     actualFormat, j;
273     unsigned long           nitems, leftover, num_workspaces=0;
274     char                    all_workspace_names[256];
275     char                    *workspace_name;
276     Display                 *display = XtDisplay(CreateActionAppShell);
277     FILE                    *fp;
278
279     DtSessionSavePath(CreateActionAppShell, &path, &name);
280 #ifdef DEBUG
281     printf("path = %s\n", path);
282     printf("name = %s\n", name);
283 #endif
284
285     /*  Create the session file  */
286     if (((fd = creat(path, S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP)) == -1) ||
287         ((fp = fdopen(fd, "w")) == (FILE *)NULL))
288     {
289        msgPtr = GETMESSAGE(5, 160, "Could not save session to file:");
290        fmtPtr = "%s\n %s\n";
291        errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
292                           strlen(path) + 1) * sizeof(char));
293        sprintf(errPtr, fmtPtr, msgPtr, path);
294        display_error_message(CreateActionAppShell, errPtr);
295        XtFree(errPtr);
296        if (fd != -1)
297        {
298            close(fd);
299            unlink(path);
300        }
301        XtFree ((char *)path);
302        XtFree ((char *)name);
303        return;
304     }
305
306     /*  Getting the WM_STATE property to see if iconified or not */
307 #ifdef DEBUG
308     printf("getting wm state\n");
309 #endif
310     wmStateAtom = XInternAtom(display, "WM_STATE", False);
311
312 #ifdef DEBUG
313     printf("getting window property\n");
314 #endif
315     XGetWindowProperty (XtDisplay(CreateActionAppShell),
316                         XtWindow(CreateActionAppShell), wmStateAtom, 0L,
317                         (long)BUFSIZ, False, wmStateAtom, &actualType,
318                         &actualFormat, &nitems, &leftover,
319                         (unsigned char **) &wmState);
320
321     if (wmState->state == IconicState)
322         fprintf(fp, "*iconic: True\n");
323     else
324         fprintf(fp, "*iconic: False\n");
325
326     fprintf(fp, "*expert: %s\n",
327             XtIsManaged(XprtOptionForm) ? "True" : "False");
328
329     /*** Get and write out the geometry info for our Window ***/
330
331 #ifdef DEBUG
332     printf("getting geometry info\n");
333 #endif
334
335     x = XtX(CreateActionAppShell);
336     y = XtY(CreateActionAppShell);
337     width = XtWidth(CreateActionAppShell);
338     height = XtHeight(CreateActionAppShell);
339
340     /*  Modify x & y to take into account window mgr frames
341      *  This is pretty bogus, but I don't know a better way to do it.
342      */
343     extData = _XmGetWidgetExtData(CreateActionAppShell, XmSHELL_EXTENSION);
344     vendorExt = (XmVendorShellExtObject)extData->widget;
345     x -= vendorExt->vendor.xOffset;
346     y -= vendorExt->vendor.yOffset;
347
348     fprintf(fp, "*x: %d\n", x);
349     fprintf(fp, "*y: %d\n", y);
350     fprintf(fp, "*width: %d\n", width);
351     fprintf(fp, "*height: %d\n", height);
352
353 #ifdef DEBUG
354     printf("getting workspaces this window is present \n");
355 #endif
356
357     if (DtWsmGetWorkspacesOccupied (display,
358                                     XtWindow(CreateActionAppShell),
359                                     &ws_presence, &num_workspaces) == Success)
360     {
361         for (j = 0; j < num_workspaces; j++)
362         {
363             workspace_name = XGetAtomName (display, ws_presence[j]);
364             if (j == 0)
365                 (void) strcpy(all_workspace_names, workspace_name);
366             else
367             {
368                 (void) strcat(all_workspace_names, "*");
369                 (void) strcat(all_workspace_names, workspace_name);
370             }
371             XFree ((char *) workspace_name);
372         }
373         XtFree((char *)ws_presence);
374         fprintf(fp, "*workspace: %s\n", all_workspace_names);
375     }
376
377     /*  This will be used to indicate what file is currently being edited. */
378     if (pszFileToEdit != NULL) {
379         fprintf(fp, "*file: %s\n", pszFileToEdit);
380     }
381
382     fflush(fp);
383     fclose(fp);
384
385     n = 0;
386     xa_CommandStr[n] = pszExecName; n++;
387     xa_CommandStr[n] =  "-s";       n++;
388     xa_CommandStr[n] = name;        n++;
389
390     XSetCommand(XtDisplay(CreateActionAppShell), XtWindow(CreateActionAppShell), xa_CommandStr, n);
391     XtFree ((char *)path);
392     XtFree ((char *)name);
393
394     /*  Don't exit yet, SM needs time to get the new commandStr.*/
395 }
396
397 /*****************************************************************************/
398 /*                                                                           */
399 /* SaveSessionCB                                                             */
400 /*                                                                           */
401 /* Session is ending, save session information                               */
402 /*                                                                           */
403 /*****************************************************************************/
404 static void
405 SaveSessionCB (
406         Widget w,
407         XtPointer clientData,
408         XtPointer callbackArg )
409 {
410   char      *xa_CommandStr[3];
411   int       i;
412
413 #if 0
414   i = 0;
415   xa_CommandStr[i] = pszExecName; i++;
416   XSetCommand(XtDisplay(CreateActionAppShell), XtWindow(CreateActionAppShell), xa_CommandStr, i);
417 #endif
418
419   SaveSession();
420 }
421
422 /*****************************************************************************/
423 /*                                                                           */
424 /*  GetIconDataFromWid                                                       */
425 /*                                                                           */
426 /*                                                                           */
427 /*****************************************************************************/
428 IconData *GetIconDataFromWid( Widget wid )
429 {
430   int       i;
431   IconData  *pIconData;
432
433   for (i=0; i < ICON_NUMBER; i++) {
434      if ((IconDataList[i]) && (IconDataList[i]->wid == wid)) {
435         return(IconDataList[i]);
436      }
437   }
438   return((IconData *)NULL);
439 }
440
441 /*****************************************************************************/
442 /*                                                                           */
443 /*  GetIconDataFromMsgID                                                     */
444 /*                                                                           */
445 /*                                                                           */
446 /*****************************************************************************/
447 IconData *GetIconDataFromMsgID( char *msgID )
448 {
449   int       i;
450   IconData  *pIconData;
451
452   for (i=0; i < ICON_NUMBER; i++) {
453      if (IconDataList[i]->pmMsgID) {
454         if (!strcmp(IconDataList[i]->pmMsgID, msgID)) {
455            return(IconDataList[i]);
456         }
457      }
458      if (IconDataList[i]->bmMsgID) {
459         if (!strcmp(IconDataList[i]->bmMsgID, msgID)) {
460            return(IconDataList[i]);
461         }
462      }
463   }
464   /*
465   for (i=0; i < ICON_NUMBER; i++) {
466      if ( ((IconDataList[i]->pmMsgID) &&
467            (!strcmp(IconDataList[i]->pmMsgID, msgID))) ||
468           ((IconDataList[i]->bmMsgID) &&
469            (!strcmp(IconDataList[i]->bmMsgID, msgID))) ) {
470         return(IconDataList[i]);
471      }
472   }
473   */
474   return((IconData *)NULL);
475 }
476
477 /*****************************************************************************/
478 /*                                                                           */
479 /*  RemoveTmpIconFiles                                                       */
480 /*                                                                           */
481 /*                                                                           */
482 /*****************************************************************************/
483 void RemoveTmpIconFiles( void )
484 {
485   int       i;
486   IconData  *pIconData;
487   char      maskFile[MAXFILENAME];
488
489   for (i=0; i < ICON_NUMBER; i++) {
490      if ( (IconDataList[i]->pmDirtyBit) &&
491           (IconDataList[i]->pmFileName) &&
492           (strlen(IconDataList[i]->pmFileName)) ) {
493 #ifdef DEBUG
494         printf("RemoveTmpIconFiles: unlink '%s'\n", IconDataList[i]->pmFileName);  /* debug */
495 #endif
496         unlink(IconDataList[i]->pmFileName);
497         /* This is bogus.  But just in case the user specified a bitmap */
498         /* rather than a pixmap during the edit process, this will make */
499         /* sure that file gets removed.  The chance of this occurring   */
500         /* is very unlikely.                                            */
501         strcpy(maskFile, IconDataList[i]->pmFileName);
502         strcat(maskFile, "_m");
503
504 #ifdef DEBUG
505         printf("Remove maskFile = %s\n", maskFile); /* debug */
506 #endif
507         unlink(maskFile);
508      }
509      if ( (IconDataList[i]->bmDirtyBit) &&
510           (IconDataList[i]->bmFileName) &&
511           (strlen(IconDataList[i]->bmFileName)) ) {
512 #ifdef DEBUG
513         printf("RemoveTmpIconFiles: unlink '%s'\n", IconDataList[i]->bmFileName);  /* debug */
514 #endif
515         unlink(IconDataList[i]->bmFileName);
516         strcpy(maskFile, IconDataList[i]->bmFileName);
517         strcat(maskFile, "_m");
518 #ifdef DEBUG
519         printf("Remove maskFile = %s\n", maskFile); /* debug */
520 #endif
521         unlink(maskFile);
522      }
523   }
524 }
525
526 #ifdef __TOOLTALK
527 /*****************************************************************************/
528 /*                                                                           */
529 /*  DieFromToolTalkError                                                     */
530 /*                                                                           */
531 /*                                                                           */
532 /*****************************************************************************/
533 static void
534 ExitCB (Widget dialog, XtPointer client_data, XtPointer call_data)
535 {
536     exit((int) client_data);
537 }
538
539 void
540 DieFromToolTalkError(Widget parent, char *errfmt, Tt_status status)
541 {
542     Arg          args[10];
543     Widget       dialog, dialogShell;
544     char        *errmsg, *statmsg, *title;
545     XmString     xms_errmsg, xms_ok, xms_title;
546     int          n;
547
548     if (! tt_is_err(status)) return;
549
550     statmsg = tt_status_message(status);
551     errmsg = XtMalloc(strlen(errfmt) + strlen(statmsg) + 2);
552     sprintf(errmsg, errfmt, statmsg);
553
554     xms_ok = GETXMSTRING(13, 3, "OK");
555     xms_errmsg = XmStringCreateLocalized(errmsg);
556     xms_title = GETXMSTRING(13, 1, "Create Action - Error");
557
558     n = 0;
559     XtSetArg(args[n], XmNautoUnmanage, False); n++;
560     XtSetArg(args[n], XmNokLabelString, xms_ok); n++;
561     XtSetArg(args[n], XmNdialogTitle, xms_title); n++;
562     XtSetArg(args[n], XmNmessageString, xms_errmsg); n++;
563     XtSetArg(args[n], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL); n++;
564
565     dialog = XmCreateErrorDialog(parent, "IconEditorError", args, n);
566     XtAddCallback(dialog, XmNokCallback, ExitCB, (XtPointer) status);
567     XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON));
568     XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
569
570     /*
571      * Disable the frame menu from dialog since we don't want the user
572      * to be able to close dialogs with the frame menu
573      */
574     dialogShell = XtParent(dialog);
575     n = 0;
576     XtSetArg(args[n], XmNmwmDecorations, MWM_DECOR_ALL | MWM_DECOR_MENU); n++;
577     XtSetValues(dialogShell, args, n);
578     XtManageChild(dialog);
579     XtRealizeWidget(dialogShell);
580
581     _DtSimpleError(CLASS_NAME, DtFatalError, NULL, errmsg);
582
583     XtFree(errmsg);
584     XmStringFree(xms_ok);
585     XmStringFree(xms_errmsg);
586     XmStringFree(xms_title);
587
588     while (TRUE)
589       XtAppProcessEvent(XtWidgetToApplicationContext(dialog), XtIMAll);
590 }
591
592 /*****************************************************************************/
593 /*                                                                           */
594 /*  ProcessToolTalkMessage                                                   */
595 /*                                                                           */
596 /*                                                                           */
597 /*****************************************************************************/
598 Tt_message ProcessToolTalkMessage(Tt_message msg,
599                                   void* cdata,
600                                   Tt_message contract )
601 {
602
603   int ttMark = tt_mark();
604
605   if( 0 == strcmp( tt_message_op( msg ), "Quit" ) ) {
606           /*
607            * Do the necessary cleanup, call tt_message_reply(),
608            * and exit.  Since this is not yet fully implemented, just
609            * call tt_message_reply and exit.
610            */
611
612 #ifdef DEBUG
613           printf("ProcessTTMsg: Quit\n");  /* debug */
614 #endif
615           tt_message_reply(msg);
616           RemoveTmpIconFiles();
617           exit(1);
618   } else {
619           /*
620            * Any other desktop message ops that one would want
621            * to implement would go here.  If
622            * a mapped, realized, top-level widget were passed into
623            * ttdt_session_join instead of (Widget) 0, then the
624            * messages that interact with the X environment
625            * Get_Geometry, etc.) would be handled automatically.
626            */
627
628 #ifdef DEBUG
629           printf("ProcessTTMsg: Other\n");
630 #endif
631
632   }
633   tt_release( ttMark );
634
635   return (Tt_message) 0;
636 }
637
638 /*****************************************************************************/
639 /* NAME:        IconEdit_tt_handler                                          */
640 /* DESCRIPTION: Handler for Tooltalk conversation about editing icon.        */
641 /* PARAMETERS:                                                               */
642 /* RETURN:      none                                                         */
643 /* CREATION:    02/21/94                                                     */
644 /* REVISIONS:                                                                */
645 /*****************************************************************************/
646 Tt_callback_action IconEdit_tt_handler( Tt_message m, Tt_pattern p )
647 {
648   int             ttMark;
649   int             blen;
650   int             wlen;
651   int             tmpfd = -1;
652   Tt_state        mstate;
653   char            *msgPtr, *fmtPtr, *errPtr;
654   char            tmpbuf[MAXBUFSIZE];
655   char*           ttbuf;
656   char*           op;
657   char            *tmpIconFile;
658   char            *msgID;
659   IconData        *pIconData;
660   Boolean         bIsNewFile = False;
661
662   char            *tmp = (char *)NULL;
663   char            *tmp1 = (char *)NULL;
664   int             tmplen;
665
666   ttMark = tt_mark( );
667
668   op = tt_message_op(m);
669   /************************************************************************/
670   /* Edit message...                                                      */
671   /************************************************************************/
672   if (!strcmp(op, "Edit")) {
673 #ifdef DEBUG
674      printf("IconEdit_tt_handler: Edit tt msg = %p\n", m);  /* debug */
675 #endif
676      mstate = tt_message_state( m );
677      switch ((int)mstate) {
678          case TT_FAILED:
679              TurnOffHourGlassAllWindows();
680
681              /* INTERNATIONALIZE */
682              msgPtr = "Failed ToolTalk msg";
683              fmtPtr = "%s = %p";
684              errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
685                                 20) * sizeof(char));
686              sprintf(errPtr, fmtPtr, msgPtr, m);
687              display_error_message(CreateActionAppShell, errPtr);
688              XtFree(errPtr);
689 #ifdef DEBUG
690              printf( "Failed ToolTalk msg = %p\n", m);   /* debug */
691 #endif
692              tt_message_destroy( m );
693              break;
694          case TT_HANDLED:
695              TurnOffHourGlassAllWindows();
696 #ifdef DEBUG
697              printf( "Handled ToolTalk msg = %p\n", m);  /* debug */
698 #endif
699              tt_message_destroy( m );
700              break;
701 #ifdef DEBUG
702          case TT_STARTED:
703              printf( "Started ToolTalk msg = %p\n", m);  /* debug */
704 #endif
705      }
706   } else {
707 #ifdef DEBUG
708      printf("IconEdit_tt_handler: Saved tt msg = %p\n", m);  /* debug */
709 #endif
710      msgID = tt_message_arg_val(m, 1);
711      pIconData = GetIconDataFromMsgID(msgID);
712 #ifdef DEBUG
713      printf("msgID = '%s'\n", msgID);  /* debug */
714 #endif
715
716      /*********************************************************************/
717      /* Saved message...                                                  */
718      /*********************************************************************/
719      if ( (pIconData) && (!strcmp(op, "Saved")) ) {
720         tt_message_arg_bval(m, 0, (unsigned char **)&ttbuf, &blen);
721
722         if ( (pIconData->pmMsgID) && (!strcmp(pIconData->pmMsgID, msgID)) ) {
723            if (pIconData->pmDirtyBit) {
724               tmpIconFile = pIconData->pmFileName;
725            } else {
726               tmpnam(pIconData->pmFileName);
727               bIsNewFile = True;
728               tmpIconFile = pIconData->pmFileName;
729            }
730         } else {
731            if (pIconData->bmDirtyBit) {
732               tmpIconFile = pIconData->bmFileName;
733            } else {
734               tmpnam(pIconData->bmFileName);
735               bIsNewFile = True;
736               tmpIconFile = pIconData->bmFileName;
737            }
738         }
739         if (bIsNewFile) {
740             tmpfd = open(tmpIconFile, O_CREAT | O_WRONLY | O_NDELAY, 0666);
741         } else {
742            tmpfd = open(tmpIconFile, O_TRUNC | O_WRONLY | O_NDELAY, 0666);
743         }
744         if (tmpfd == -1) {
745            msgPtr = GETMESSAGE(5, 60, "Could not open the following file:");
746            fmtPtr = "%s\n   %s";
747            errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
748                               strlen(tmpIconFile) + 1) * sizeof(char));
749            sprintf(errPtr, fmtPtr, msgPtr, tmpIconFile);
750            display_error_message(XtParent(pIconData->wid), errPtr);
751            XtFree(errPtr);
752            tt_release( ttMark );
753            return;
754         }
755
756         /*******************************************************************/
757         /* Determine if this is a pixmap or bitmap.  If it is a pixmap,    */
758         /* handle as a one file buffer.  If a bitmap, then it may be       */
759         /* double buffered including the mask.                             */
760         /*******************************************************************/
761
762         /*******************************************************************/
763         /* Check for Pixmap first.                                         */
764         /*******************************************************************/
765         if ( (ttbuf) && (!strncmp(ttbuf, "/* XPM */", 9)) ) {
766 #ifdef DEBUG
767            printf("This is a XPM file\n");  /* debug */
768 #endif
769            tmp = strstr(ttbuf, "};");
770            if (tmp) {
771               tmp += 2;                        /* Go one char past the ";" */
772               /*
773               tmplen = blen - strlen(tmp) + 1;
774               printf("Amro tmplen = %d\n", tmplen);
775               */
776               tmplen = (char *)tmp - (char *)ttbuf;
777 #ifdef DEBUG
778               printf("Troy tmplen = %d\n", tmplen); /* debug */
779 #endif
780            } else {
781               tmplen = blen;
782            }
783            if ((wlen = write(tmpfd, ttbuf, tmplen)) == tmplen) {
784               if (tmpfd > -1) close(tmpfd);
785            } else {
786               /* INTERNATIONALIZE */
787               msgPtr = "Could not write to the following file:";
788               fmtPtr = "%s\n   %s";
789               errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
790                                  strlen(tmpIconFile) + 1) * sizeof(char));
791               sprintf(errPtr, fmtPtr, msgPtr, tmpIconFile);
792               display_error_message(XtParent(pIconData->wid), errPtr);
793               XtFree(errPtr);
794               tt_release( ttMark );
795               if (tmpfd > -1) close(tmpfd);
796               return;
797            }
798
799         /*******************************************************************/
800         /* Must be a Bitmap.                                               */
801         /*******************************************************************/
802         } else {
803 #ifdef DEBUG
804            printf("This is a XBM file\n");  /* debug */
805            printf("ttbuffer size = %d\n", blen); /* debug */
806 #endif
807            /****************************************************************/
808            /* Determine size of first bitmap.                              */
809            /****************************************************************/
810            tmp = strstr(ttbuf, "};");
811            if (tmp) {
812               tmp += 2;                        /* Go one char past the ";" */
813               /*
814               tmplen = blen - strlen(tmp) + 1;
815               printf("Amro tmplen = %d\n", tmplen);
816               */
817               tmplen = (char *)tmp - (char *)ttbuf;
818 #ifdef DEBUG
819               printf("Troy tmplen = %d\n", tmplen); /* debug */
820 #endif
821            } else {
822               tmplen = blen;
823            }
824 #ifdef DEBUG
825            printf("File 1 size = %d\n", tmplen); /* debug */
826 #endif
827
828            /****************************************************************/
829            /* Write out first bitmap file.                                 */
830            /****************************************************************/
831 #ifdef DEBUG
832            printf("Writing first bitmap file\n"); /* debug */
833 #endif
834            if ((wlen = write(tmpfd, ttbuf, tmplen)) == tmplen) {
835               if (tmpfd > -1) close(tmpfd);
836            } else {
837               /* INTERNATIONALIZE */
838               msgPtr = "Could not write to the following file:";
839               fmtPtr = "%s\n   %s";
840               errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
841                                  strlen(tmpIconFile) + 1) * sizeof(char));
842               sprintf(errPtr, fmtPtr, msgPtr, tmpIconFile);
843               display_error_message(XtParent(pIconData->wid), errPtr);
844               XtFree(errPtr);
845               tt_release( ttMark );
846               if (tmpfd > -1) close(tmpfd);
847               return;
848            }
849
850            /****************************************************************/
851            /* Now determine if there is a second bitmap and its size.      */
852            /****************************************************************/
853            if (tmp) {
854               tmp1 = strstr(tmp, "};");
855            }
856            sprintf(tmpbuf, "%s_m", tmpIconFile);
857            if (tmp1) {
858               tmp1 += 2;                       /* Go one char past the ";" */
859 #ifdef DEBUG
860               printf("Mask file name = '%s'\n", tmpbuf); /* debug */
861 #endif
862               tmpfd = open(tmpbuf, O_CREAT | O_WRONLY | O_NDELAY, 0666);
863               if (tmpfd == -1) {
864                  msgPtr =
865                      GETMESSAGE(5, 60, "Could not open the following file:");
866                  fmtPtr = "%s\n   %s";
867                  errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
868                                     strlen(tmpbuf) + 1) * sizeof(char));
869                  sprintf(errPtr, fmtPtr, msgPtr, tmpbuf);
870                  display_error_message(XtParent(pIconData->wid), errPtr);
871                  XtFree(errPtr);
872                  tt_release( ttMark );
873                  return;
874               }
875
876 #ifdef DEBUG
877               printf("writing mask file \n");
878 #endif
879               /*
880               tmplen = strlen(tmp);
881               printf("Amro tmplen = %d\n", tmplen);
882               */
883               tmplen = (char *)tmp1 - (char *)tmp;
884 #ifdef DEBUG
885               printf("Troy tmplen = %d\n", tmplen); /* debug */
886 #endif
887               if ((wlen = write(tmpfd, tmp, tmplen)) == tmplen) {
888                  if (tmpfd > -1) close(tmpfd);
889               } else {
890                  /* INTERNATIONALIZE */
891                  msgPtr = "Could not write to the following file:";
892                  fmtPtr = "%s\n   %s";
893                  errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
894                                     strlen(tmpbuf) + 1) * sizeof(char));
895                  sprintf(errPtr, fmtPtr, msgPtr, tmpbuf);
896                  display_error_message(XtParent(pIconData->wid), errPtr);
897                  XtFree(errPtr);
898                  tt_release( ttMark );
899                  if (tmpfd > -1) close(tmpfd);
900                  return;
901               }
902
903            /****************************************************************/
904            /* Must not have a mask, so lets clean up any old mask file.    */
905            /****************************************************************/
906            } else {
907 #ifdef DEBUG
908               printf("Remove maskFile = %s\n", tmpbuf); /* debug */
909 #endif
910               unlink(tmpbuf);
911            }
912         }
913
914         /****************************************************************/
915         /* Set the icon gadget with new icon, and flag the dirty bit.   */
916         /****************************************************************/
917         if ( ( (pIconData->pmMsgID) &&
918                (!strcmp(pIconData->pmMsgID, msgID)) &&
919                (bShowPixmaps) )
920            ||
921              ( (pIconData->bmMsgID) &&
922                (!strcmp(pIconData->bmMsgID, msgID)) &&
923                (!bShowPixmaps) ) ) {
924            XmeFlushIconFileCache(NULL);
925 #ifdef DEBUG
926            printf("Change icon image for widget = %p\n", pIconData->wid); /* debug */
927 #endif
928            SET_ICONGADGET_ICON(pIconData->wid, tmpIconFile);
929         }
930         if ((pIconData->pmMsgID) && (!strcmp(pIconData->pmMsgID, msgID))) {
931            pIconData->pmDirtyBit = True;
932         } else {
933            pIconData->bmDirtyBit = True;
934         }
935 #ifdef DEBUG
936         printf("**********tmp file name = %s\n", tmpIconFile);  /* debug */
937 #endif
938      }
939   }
940   tt_release( ttMark );
941   return( TT_CALLBACK_PROCESSED );
942 }
943
944 /*****************************************************************************/
945 /* NAME:        UxDoEditPixmap                                               */
946 /* DESCRIPTION: Loads the icon editor.                                       */
947 /* PARAMETERS:  none                                                         */
948 /* RETURN:      none                                                         */
949 /* CREATION:    02/11/94                                                     */
950 /* REVISIONS:                                                                */
951 /*****************************************************************************/
952 void UxDoEditPixmap(Widget wid, char *fname)
953 {
954   Tt_message     m;         /* ToolTalk message */
955   Tt_pattern     p;         /* ToolTalk dynamic pattern */
956   Tt_status      ttRc;      /* ToolTalk status  */
957
958   int            fd1 = -1;
959   int            fd2 = -1;
960
961   int            lenFile1 = 0, rlenFile1 = 0;
962   int            lenFile2 = 0, rlenFile2 = 0;
963
964   int            mark;
965   char           *msgID;
966   char           **ppMsgID;
967   IconData       *pIconData;
968   unsigned char  *buffer;
969   char           *ptr;
970
971   char           tmpbuf[MAXBUFSIZE];
972   char           *errPtr;
973   char           *msgPtr;
974   char           *fmtPtr;
975   char           *mname = (char *)NULL;
976   char           *type_name, *size_name;
977
978
979 #ifdef DEBUG
980   if (fname) printf("Edit filename: '%s'\n", fname);  /* debug */
981 #endif
982
983   mark = tt_mark();
984
985   /*********************************************************************/
986   /* Create request                                                    */
987   /*********************************************************************/
988   m = tt_prequest_create( TT_SESSION, "Edit" );
989   msgID = tt_message_id(m);
990   pIconData = GetIconDataFromWid(wid);
991   if (bShowPixmaps) {
992      ppMsgID = &(pIconData->pmMsgID);
993   } else {
994      ppMsgID = &(pIconData->bmMsgID);
995   }
996   if (*ppMsgID) {
997      free(*ppMsgID);
998   }
999   if (msgID && strlen(msgID)) {
1000      *ppMsgID = (char *)malloc(strlen(msgID) + 1);
1001      strcpy(*ppMsgID, msgID);
1002   }
1003
1004 #ifdef DEBUG
1005   printf("msgID = '%s'\n", msgID);  /* debug */
1006   printf("Create Edit msg = %p\n", m);  /* debug */
1007 #endif
1008
1009   /*********************************************************************/
1010   /* Add arguments to message                                          */
1011   /*********************************************************************/
1012   if (fname) {
1013      fd1 = open(fname, O_RDONLY);
1014      if (fd1 == -1) {
1015         /* Error opening icon file! */
1016         msgPtr = GETMESSAGE(5, 60, "Could not open the following file:");
1017         fmtPtr = "%s\n   %s";
1018         errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
1019                            strlen(fname) + 1) * sizeof(char));
1020         sprintf(errPtr, fmtPtr, msgPtr, fname);
1021         display_error_message(XtParent(wid), errPtr);
1022         XtFree(errPtr);
1023         return;
1024      } else {
1025         /***************************************/
1026         /* Find out how big a buffer is needed */
1027         /***************************************/
1028         lenFile1 = lseek(fd1, 0, SEEK_END);
1029         (void) lseek(fd1, 0, SEEK_SET);
1030
1031         /*************************************************************/
1032         /* Read first few bytes to determine what kind of file it is */
1033         /*************************************************************/
1034         read(fd1, tmpbuf, 10);
1035         (void) lseek(fd1, 0, SEEK_SET);
1036
1037         /******************************************************************/
1038         /* If the file is a bitmap, then check for its mask file and add. */
1039         /******************************************************************/
1040         if ( strncmp(tmpbuf, "/* XPM */", 9) ) {
1041
1042            mname = CreateMaskName(fname);
1043
1044            /***************************************************************/
1045            /* Check if mask file exists.                                  */
1046            /***************************************************************/
1047            if ((mname) && (check_file_exists(mname))) {
1048               fd2 = open(mname, O_RDONLY);
1049               if (fd2 == -1) {
1050                  /* Error opening icon file! */
1051                  msgPtr =
1052                      GETMESSAGE(5, 60, "Could not open the following file:");
1053                  fmtPtr = "%s\n   %s";
1054                  errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
1055                                     strlen(mname) + 1) * sizeof(char));
1056                  sprintf(errPtr, fmtPtr, msgPtr, mname);
1057                  display_error_message(XtParent(wid), errPtr);
1058                  XtFree(errPtr);
1059                  XtFree(mname);
1060                  return;
1061               } else {
1062                  /***************************************/
1063                  /* Find out how big a buffer is needed */
1064                  /***************************************/
1065                  lenFile2 = lseek(fd2, 0, SEEK_END);
1066                  (void) lseek(fd2, 0, SEEK_SET);
1067               }
1068            }
1069         }
1070         /******************************************************************/
1071         /* Create 1 big buffer                                            */
1072         /******************************************************************/
1073 #ifdef DEBUG
1074         printf("Size of file 1 = %d\n", lenFile1); /* debug */
1075         printf("Size of file 2 = %d\n", lenFile2); /* debug */
1076 #endif
1077         buffer = (unsigned char *)XtMalloc((Cardinal)(lenFile1 + lenFile2 + 1));
1078         memset(buffer, 0, (lenFile1 + lenFile2 + 1));
1079         if (buffer) {
1080            /***************************************/
1081            /* Read file1 into buffer.             */
1082            /***************************************/
1083            if ((rlenFile1 = read(fd1, buffer, lenFile1)) != lenFile1) {
1084               /* didn't read whole file! */
1085
1086               /* INTERNATIONALIZE */
1087               msgPtr = "Could not read the entire file:";
1088               fmtPtr = "%s\n   %s";
1089               errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
1090                                  strlen(fname) + 1) * sizeof(char));
1091               sprintf(errPtr, fmtPtr, msgPtr, fname);
1092               display_error_message(XtParent(wid), errPtr);
1093               XtFree(errPtr);
1094               if (mname) XtFree(mname);
1095               XtFree((char *) buffer);
1096               return;
1097            }
1098            if (lenFile2) {
1099 #ifdef DEBUG
1100               printf("buffer = %p\n", buffer);
1101 #endif
1102             /*ptr = (char *)&(buffer[lenFile1 + 1]);*/
1103               ptr = (char *)&(buffer[lenFile1]);
1104 #ifdef DEBUG
1105               printf("ptr = %p\n", ptr);
1106 #endif
1107               if ((rlenFile2 = read(fd2, ptr, lenFile2)) != lenFile2) {
1108                  /* didn't read whole file! */
1109
1110                  /* INTERNATIONALIZE */
1111                  msgPtr = "Could not read the entire file:";
1112                  fmtPtr = "%s\n   %s";
1113                  errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
1114                                     strlen(fname) + 1) * sizeof(char));
1115                  sprintf(errPtr, fmtPtr, msgPtr, mname);
1116                  display_error_message(XtParent(wid), errPtr);
1117                  XtFree(errPtr);
1118                  XtFree((char *) buffer);
1119                  if (mname) XtFree(mname);
1120                  return;
1121               }
1122            }
1123            if (fd1 > -1) close(fd1);
1124            if (fd2 > -1) close(fd2);
1125
1126 #ifdef DEBUG
1127            printf("final buffer = '%s'\n", buffer); /* debug */
1128            printf("Buffer = '%s'\n", buffer); /* debug */
1129 #endif
1130            tt_message_barg_add( m, TT_INOUT, "XPM", buffer, lenFile1 + lenFile2 + 1);
1131            if (mname) XtFree(mname);
1132            XtFree((char *) buffer);
1133         }
1134      }
1135   } else {
1136      tt_message_barg_add( m, TT_INOUT, "XPM", (const unsigned char *)NULL, 0);
1137   }
1138   tt_message_arg_add(m, TT_IN, "messageID", msgID);
1139
1140   /******************************************************************/
1141   /* Create a pattern and add a callback to get notifications       */
1142   /* of periodic saves                                              */
1143   /******************************************************************/
1144   p = tt_pattern_create();
1145   tt_pattern_category_set(p, TT_OBSERVE);
1146   tt_pattern_scope_add(p, TT_SESSION);
1147   tt_pattern_session_add(p, tt_default_session());
1148   tt_pattern_address_add(p, TT_HANDLER);
1149   tt_pattern_op_add(p, "Saved");
1150   tt_pattern_callback_add(p, IconEdit_tt_handler);
1151   tt_pattern_register(p);
1152
1153   /******************************************************************/
1154   /* Add callback to notify when change complete                    */
1155   /******************************************************************/
1156   tt_message_callback_add( m, IconEdit_tt_handler );
1157
1158   /******************************************************************/
1159   /* Send message                                                   */
1160   /******************************************************************/
1161   TurnOnHourGlassAllWindows();
1162   ttRc = tt_message_send( m );
1163   DieFromToolTalkError( UxTopLevel, "tt_message_send", ttRc );
1164 }
1165 #endif  /* __TOOLTALK */
1166
1167 /*****************************************************************************/
1168 /*                                                                           */
1169 /*                                                                           */
1170 /*    main                                                                   */
1171 /*                                                                           */
1172 /*                                                                           */
1173 /*                                                                           */
1174 /*****************************************************************************/
1175 main(int argc, char *argv[])
1176 {
1177         /*-----------------------------------------------------------
1178          * Declarations.
1179          * The default identifier - mainIface will only be declared
1180          * if the interface function is global and of type swidget.
1181          * To change the identifier to a different name, modify the
1182          * string mainIface in the file "xtmain.dat". If "mainIface"
1183          * is declared, it will be used below where the return value
1184          * of  PJ_INTERFACE_FUNCTION_CALL will be assigned to it.
1185          *----------------------------------------------------------*/
1186 #ifdef __TOOLTALK
1187         char * procId;
1188         int ttMark, ttFd;
1189         Tt_pattern *ttPatt;
1190         Tt_status ttRc;
1191         char * errfmt;
1192         char * sessionString;
1193         char * ttsession;
1194 #endif
1195
1196         Widget          mainIface;
1197         extern swidget  create_AddFiletype();
1198         extern swidget  create_FileCharacteristics();
1199         extern swidget  create_OpenFile();
1200         extern swidget  create_Confirmed();
1201         extern Widget   create_ErrorDialog();
1202         int             rc;
1203         Atom            xa_WM_SAVE_YOURSELF;
1204         char            *ptr;
1205
1206         /*---------------------------------
1207          * Interface function declaration
1208          *--------------------------------*/
1209         Widget  create_applicationShell1(swidget);
1210         swidget UxParent = NULL;
1211         int i;
1212
1213         /*---------------------
1214          * Initialize program
1215          *--------------------*/
1216
1217         _DtEnvControl(DT_ENV_SET);
1218         XtSetLanguageProc(NULL, NULL, NULL);
1219         pszFileToEdit = (char *)NULL;
1220         memset(&sessiondata, 0, sizeof(SessionData));
1221         memset(IconDataList, 0, sizeof(IconDataList));
1222
1223         /********************************************************************/
1224         /* Parse the command line arguments.                                */
1225         /********************************************************************/
1226         pszExecName = argv[0];
1227
1228         if (argc > 1) {
1229           for (i=1; i<argc; i++) {
1230             if ((argv[i][0] == '-') && (argv[i][1] == '?')) {
1231                setlocale(LC_ALL, "");
1232                nlmsg_fd = catopen("dtcreate", NL_CAT_LOCALE);
1233                printf("%s\n",
1234                       GETMESSAGE(5, 110, "Usage: dtcreate [<filename>] [-?]"));
1235                exit(0);
1236             } else {
1237                if (argv[i][0] == '-') {
1238                   if (!strcmp(argv[i], "-s")) {
1239                      i+=1;
1240                      sessiondata.pszSessionFile = (char *)XtMalloc(strlen(argv[i]) + 1);
1241                      strcpy(sessiondata.pszSessionFile, argv[i]);
1242                      sessiondata.useSession = True;
1243                   } else {
1244                      i+=1;
1245                   }
1246                } else {
1247                   pszFileToEdit = (char *)XtMalloc(strlen(argv[i]) + 1);
1248                   strcpy(pszFileToEdit, argv[i]);
1249                }
1250             }
1251           }
1252         }
1253
1254
1255         UxTopLevel = XtAppInitialize(&UxAppContext, CLASS_NAME,
1256                                      NULL, 0, &argc, argv, NULL, NULL, 0);
1257
1258         nlmsg_fd = catopen("dtcreate", NL_CAT_LOCALE);
1259
1260         UxDisplay = XtDisplay(UxTopLevel);
1261         UxScreen = XDefaultScreen(UxDisplay);
1262
1263         if (sessiondata.useSession) {
1264            GetSessionInfo();
1265         }
1266
1267         /*---------------------
1268          * Determine resolution
1269          *--------------------*/
1270         if (_DtGetDisplayResolution(UxDisplay, UxScreen) == 1) {
1271            bLowRes = True;
1272         } else {
1273            bLowRes = False;
1274         }
1275
1276 #ifdef DEBUG_LOWRES
1277         bLowRes = True;
1278 #endif
1279
1280         /*-------------------------------------------------------
1281          * Insert initialization code for your application here
1282          *------------------------------------------------------*/
1283 #ifdef __TOOLTALK
1284 #ifdef DEBUG
1285         printf("display string = '%s'\n", DisplayString(UxDisplay));
1286 #endif
1287         ttsession = getenv("TT_SESSION");
1288         if (!ttsession || strlen(ttsession)==0) {
1289            char *ttxsession = tt_X_session(XDisplayString(UxDisplay));
1290            ttRc = tt_default_session_set(ttxsession);
1291         }
1292
1293         ttMark = tt_mark( );
1294
1295         procId = tt_open( );
1296         ttRc = tt_ptr_error(procId);
1297         errfmt =
1298           GETMESSAGE(13, 2, "Could not connect to ToolTalk:\n%s\nExiting ...");
1299         DieFromToolTalkError( UxTopLevel, errfmt, ttRc );
1300
1301         ttFd = tt_fd( );
1302         DieFromToolTalkError( UxTopLevel, "tt_fd", tt_int_error( ttFd ) );
1303
1304         ttRc = tt_ptype_declare( "dtcreate" );
1305         DieFromToolTalkError( UxTopLevel, "tt_ptype_declare", ttRc );
1306
1307         ttRc = tt_session_join( tt_default_session( ));
1308         DieFromToolTalkError( UxTopLevel, "tt_session_join", ttRc );
1309
1310         XtAppAddInput( UxAppContext, ttFd, (XtPointer)XtInputReadMask,
1311                             (XtInputCallbackProc)ProcessToolTalkMessage, 0 );
1312 #endif
1313
1314         if (!pszFileToEdit) {
1315            initAD(&AD);
1316         }
1317
1318
1319         /*----------------------------------------------------------------
1320          * Create and popup the first window of the interface.  The
1321          * return value can be used in the popdown or destroy functions.
1322          * The Widget return value of  PJ_INTERFACE_FUNCTION_CALL will
1323          * be assigned to "mainIface" from  PJ_INTERFACE_RETVAL_TYPE.
1324          *---------------------------------------------------------------*/
1325
1326         mainIface = create_applicationShell1(UxParent);
1327         AddFiletype = create_AddFiletype(mainIface);
1328         FileCharacteristics = create_FileCharacteristics(AddFiletype);
1329         OpenFile = create_OpenFile();
1330
1331         UxPopupInterface(mainIface, no_grab);
1332         if (pszFileToEdit) {
1333            if (!OpenDefinitionFile(pszFileToEdit, &AD)) {
1334               writeCAToGUI(&AD);
1335            }
1336         }
1337         XtVaGetValues (CA_WindowTypeArea, XmNheight, &wintypeheight, NULL);
1338
1339         /*------------------------------------------------------------------*/
1340         /*   Register for save and restore.                                 */
1341         /*------------------------------------------------------------------*/
1342         xa_WM_SAVE_YOURSELF = XInternAtom(XtDisplay(CreateActionAppShell), "WM_SAVE_YOURSELF", False);
1343         XmAddWMProtocols(CreateActionAppShell, &xa_WM_SAVE_YOURSELF, 1);
1344         XmAddWMProtocolCallback(CreateActionAppShell, xa_WM_SAVE_YOURSELF,
1345                                 SaveSessionCB, (XtPointer)CreateActionAppShell);
1346
1347         /*------------------------------------------------------------------*/
1348         /* Make a copy of the AD data structure.  This will be used later   */
1349         /* when the user wants to exit to determine if the latest data has  */
1350         /* been saved or not.                                               */
1351         /*------------------------------------------------------------------*/
1352         pMyCopyAD = copyAD(&AD);
1353
1354 #ifdef __TOOLTALK
1355         DtDbLoad();
1356         ttMark = tt_mark( );
1357
1358         procId = ttdt_open( &ttFd, "dtcreate", "CDE", "1.0", 1 );
1359         ttRc = tt_ptr_error(procId);
1360         errfmt =
1361           GETMESSAGE(13, 2, "Could not connect to ToolTalk:\n%s\nExiting ...");
1362         DieFromToolTalkError( UxTopLevel, errfmt, ttRc );
1363
1364         ttFd = tt_fd( );
1365         ttRc = tt_int_error( ttFd );
1366         DieFromToolTalkError( UxTopLevel, "tt_fd", ttRc );
1367
1368         sessionString = tt_default_session();
1369         ttRc = tt_ptr_error( sessionString );
1370         DieFromToolTalkError( UxTopLevel, "tt_default_session", ttRc );
1371
1372         ttPatt = ttdt_session_join( sessionString, ProcessToolTalkMessage,
1373                                     (Widget) 0, (void *) 0, 1 );
1374         ttRc = tt_ptr_error(ttPatt);
1375         DieFromToolTalkError( UxTopLevel, "ttdt_session_join", ttRc );
1376
1377         XtAppAddInput( UxAppContext, ttFd, (XtPointer)XtInputReadMask,
1378                             tttk_Xt_input_handler, 0 );
1379 #endif
1380
1381         /*-----------------------
1382          * Enter the event loop
1383          *----------------------*/
1384
1385         XtAppMainLoop (UxAppContext);
1386
1387 #ifdef __TOOLTALK
1388         ttdt_session_quit(0, 0, 1);
1389         tt_close( );
1390         tt_release( ttMark );
1391         RemoveTmpIconFiles();
1392 #endif
1393 }