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