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