1 /*********************************************************************/
2 /*  bibView: Administration of BibTeX-Databases                      */
3 /*           (Verwaltung von BibTeX-Literaturdatenbanken)            */
4 /*                                                                   */
5 /*  Module:  ctl_open.c                                              */
6 /*                                                                   */
7 /*             Open Control                                          */
8 /*             - Menu function New                                   */
9 /*             - Menu function Open                                  */
10 /*                                                                   */
11 /*  Author:  Holger Martin,  martinh@informatik.tu-muenchen.de       */
12 /*           Peter M. Urban, urban@informatik.tu-muenchen.de         */
13 /*                                                                   */
14 /*  History:                                                         */
15 /*    12.05.91  PMU  created                                         */
16 /*    05.26.92       Version 1.0 released                            */
17 /*                                                                   */
18 /*  Copyright 1992 TU MUENCHEN                                       */
19 /*    See ./Copyright for complete rights and liability information. */
20 /*                                                                   */
21 /*********************************************************************/
22 
23 #include <stdio.h>
24 #include <X11/Intrinsic.h>
25 #include <X11/StringDefs.h>
26 #include <X11/Shell.h>
27 #include <X11/Xaw/Cardinals.h>
28 #include <X11/Xaw/Command.h>
29 #include <X11/Xaw/Dialog.h>
30 #include <X11/Xaw/Label.h>
31 #include <X11/Xaw/Box.h>
32 #include "FileNom.h"
33 #include "bibview.h"
34 
35 
36 /* macros and definitions */
37 /* ---------------------- */
38 #define NN_FILENAME	"noname"
39 #define NN_FILEEXT	".bib"
40 #define NN_FILENAMELEN  14
41 
42 
43 /* imported global variables */
44 /* ------------------------- */
45 extern XtAppContext app_context;
46 extern Widget topLevel, mainMenu, desktop;
47 extern Pixmap questPixmap;
48 
49 extern int sortedby;
50 
51 extern char *actual_path;
52 
53 
54 /* exported global variables */
55 /* ------------------------- */
56 
57 
58 /* local function prototypes */
59 /* ------------------------- */
60 static void LoadBibFile (Widget w, XtPointer clientData, XtPointer callData);
61 static void CancelLoad (Widget w, XtPointer clientData, XtPointer callData);
62 static void confirmLoad (BibPtr bp);
63 static void cancelLoadCmd (Widget w, XtPointer clientData, XtPointer callData);
64 static void loadCmdOk (Widget w, XtPointer clientData, XtPointer callData);
65 
66 
67 /* local global variables */
68 /* ---------------------- */
69 static int nnCnt = 0;
70 static BibPtr gbp = NULL;
71 
72 
73 /*********************************************************************/
74 /* copNewCmd:                                                        */
75 /*    Callback function for option new in main window file menu      */
76 /*********************************************************************/
77 void
copNewCmd(Widget w,XtPointer clientData,XtPointer callData)78 copNewCmd (Widget w, XtPointer clientData, XtPointer callData)
79 {
80 BibPtr bp;
81 FILE *tempFile;
82 int status;
83 char *tempName;
84 
85    /* make new global data struct for bib file */
86    if ((status = glbNewBibListEl(&bp)) != OK) {
87       guwError(status);
88       return;
89    }
90 
91    /* build new noname filename */
92    sprintf(bp->filename, "%s%d%s", NN_FILENAME, nnCnt+1, NN_FILEEXT);
93    /*
94 #ifdef SYSV
95    (void) getcwd(bp->filepath, MAX_FILEPATHLEN);
96 #else
97    (void) getwd(bp->filepath, MAX_FILEPATHLEN);
98 #endif
99    */
100    strcpy(bp->filepath, actual_path);
101    strcat(bp->filepath, "/");
102    strcat(bp->filepath, bp->filename);
103 
104    bp->ew = NULL;
105    bp->mw = NULL;
106    bp->lw = NULL;
107    bp->sortedby = sortedby;
108 
109    /* create temp file */
110 #ifdef NO_TEMPNAM
111    tempName = (char *)tmpnam(NULL);
112    bp->tempfile = (char *)XtMalloc(strlen(tempName)+1);
113    strcpy(bp->tempfile,tempName);
114 #else
115    bp->tempfile = (char *)tempnam(NULL, NULL);
116 #endif
117    tempFile = fopen(bp->tempfile, "w" );
118    fclose(tempFile);
119 
120    /* display window for bib */
121    if ((status = gubOpenBibWin(bp)) != OK) {
122       guwError(status);
123       return;
124    }
125    nnCnt++;
126 }
127 
128 
129 /*********************************************************************/
130 /* copOpenCmd:                                                       */
131 /*    Callback function for option open in main window file menu     */
132 /*********************************************************************/
133 void
copOpenCmd(Widget w,XtPointer clientData,XtPointer callData)134 copOpenCmd (Widget w, XtPointer clientData, XtPointer callData)
135 {
136 static Widget fsbShell, fsbBox, fsbDialog;
137 Position dx, dy, x, y;
138 
139    XtVaGetValues(desktop,
140                  XtNx, &dx,
141                  XtNy, &dy, NULL);
142    XtTranslateCoords(desktop,
143                      (Position)dx + SUBWIN_MARGIN,
144                      (Position)dy + SUBWIN_MARGIN,
145                      &x, &y);
146    fsbShell  = XtVaCreatePopupShell("fileSelectBoxShell",
147 	         topLevelShellWidgetClass, desktop,
148 	 	 XtNx, x, XtNy, y, NULL);
149    fsbBox    = XtVaCreateManagedWidget("fileSelectBox",
150                  boxWidgetClass, fsbShell, NULL);
151    fsbDialog = XtVaCreateManagedWidget("loadFileBoxShell",
152                 fileNominatorWidgetClass, fsbBox,
153                 XtNborderWidth, 0, NULL);
154 
155    XtAddCallback(fsbDialog, XtNcancelCallback, CancelLoad, fsbShell);
156    XtAddCallback(fsbDialog, XtNselectCallback, LoadBibFile, fsbDialog);
157 
158    XtSetSensitive(mainMenu, FALSE);
159    gubSetSensitive(NULL, FALSE);
160    XtPopup(fsbShell, XtGrabNonexclusive);
161 }
162 
163 
164 
165 /*********************************************************************/
166 /* LOCAL FUNCTIONS                                                   */
167 /*********************************************************************/
168 
169 /*********************************************************************/
170 /* CancelLoad:                                                       */
171 /*    Callback function for CANCEL button in file select box         */
172 /*********************************************************************/
173 static void
CancelLoad(Widget w,XtPointer clientData,XtPointer callData)174 CancelLoad (Widget w, XtPointer clientData, XtPointer callData)
175 {
176 Widget dialog = (Widget)clientData;
177 
178    XtSetSensitive(mainMenu, TRUE);
179    gubSetSensitive(NULL, TRUE);
180    XtPopdown(dialog);
181 
182 }
183 
184 
185 /*********************************************************************/
186 /* LoadBibFile:                                                      */
187 /*    Callback function for OK button in file select box             */
188 /*********************************************************************/
189 static void
LoadBibFile(Widget w,XtPointer clientData,XtPointer callData)190 LoadBibFile (Widget w, XtPointer clientData, XtPointer callData)
191 {
192 Widget dialog = (Widget)clientData;
193 String filepath;
194 BibPtr p, bp;
195 Errcode status;
196 
197    /* get and keep filename */
198    filepath = (String)FileNominatorGetFullFileName(dialog);
199    if (filepath == NULL){
200       XtPopdown(XtParent(XtParent(dialog)));
201       guwError(NO_VALID_FILENAME);
202       return;
203       }
204 
205    /* check if opened already */
206    p = glbFirstBibListEl();
207    while (p != NULL) {
208       if (strcmp(p->filepath, filepath) == 0) {
209 	 XtSetSensitive(mainMenu, TRUE);
210 	 gubSetSensitive(NULL, TRUE);
211 	 XtPopdown(XtParent(XtParent(dialog)));
212 	 confirmLoad(p);
213 	 return;
214       }
215       p = glbNextBibListEl(p);
216    }
217 
218    /* make new global data struct for bib file */
219    if ((status = glbNewBibListEl(&bp)) != OK) {
220       guwError(status);
221       XtSetSensitive(mainMenu, TRUE);
222       gubSetSensitive(NULL, TRUE);
223       XtPopdown(XtParent(XtParent(dialog)));
224       return;
225    }
226    strcpy(bp->filepath, filepath);
227    strcpy(bp->filename, (String)FileNominatorGetFileName(dialog));
228    bp->mw=NULL;
229    bp->ew=NULL;
230    bp->lw=NULL;
231 
232    bp->sortedby = sortedby;
233 
234    /* remove file select box */
235    XtSetSensitive(mainMenu, TRUE);
236    gubSetSensitive(NULL, TRUE);
237    XtPopdown(XtParent(XtParent(dialog)));
238 
239    glbReadFileOpenBib(bp);
240 
241 }
242 
243 
244 /*********************************************************************/
245 /* confirmLoad:                                                      */
246 /*    Opens dialogbox for user to confirm loading from disk          */
247 /*********************************************************************/
248 static void
confirmLoad(BibPtr bp)249 confirmLoad (BibPtr bp)
250 {
251 static Widget conShell, conDialog, conOKButton, conQuitButton;
252 Position dx, dy, x, y;
253 
254    XtVaGetValues(desktop,
255                  XtNx, &dx,
256                  XtNy, &dy, NULL);
257    XtTranslateCoords(desktop,
258                      (Position)dx + SUBWIN_MARGIN,
259                      (Position)dy + SUBWIN_MARGIN,
260                      &x, &y);
261    conShell = XtVaCreatePopupShell("confirmLoadShell",
262 	        transientShellWidgetClass, topLevel,
263 		XtNx, x, XtNy, y, NULL);
264    conDialog = XtVaCreateManagedWidget("confirmLoadBox",
265 	         dialogWidgetClass, conShell,
266 		 XtNicon, questPixmap, NULL);
267    conOKButton = XtVaCreateManagedWidget("ok",
268 	           commandWidgetClass, conDialog, NULL);
269    conQuitButton = XtVaCreateManagedWidget("cancel",
270 	           commandWidgetClass, conDialog, NULL);
271    XtAddCallback(conQuitButton, XtNcallback, cancelLoadCmd, conShell);
272    XtAddCallback(conOKButton, XtNcallback, loadCmdOk, conShell);
273 
274    XtSetSensitive(mainMenu, FALSE);
275    gubSetSensitive(NULL, FALSE);
276    gbp = bp;
277    XtPopup(conShell, XtGrabNonexclusive);
278 }
279 
280 
281 /*********************************************************************/
282 /* cancelLoadCmd:                                                    */
283 /*    Callback function for CANCEL button in confirm box             */
284 /*********************************************************************/
285 static void
cancelLoadCmd(Widget w,XtPointer clientData,XtPointer callData)286 cancelLoadCmd (Widget w, XtPointer clientData, XtPointer callData)
287 {
288 Widget shell = (Widget)clientData;
289 
290    XtSetSensitive(mainMenu, TRUE);
291    gubSetSensitive(NULL, TRUE);
292    XtPopdown(shell);
293    gbp = NULL;
294 }
295 
296 
297 /*********************************************************************/
298 /* loadCmdOK:                                                        */
299 /*    Callback function for OK button in confirm box                 */
300 /*********************************************************************/
301 static void
loadCmdOk(Widget w,XtPointer clientData,XtPointer callData)302 loadCmdOk (Widget w, XtPointer clientData, XtPointer callData)
303 {
304 Widget shell = (Widget) clientData;
305 BibPtr bp;
306 Errcode status;
307 
308    /* make new global data struct for bib file */
309    if ((status = glbNewBibListEl(&bp)) != OK) {
310       guwError(status);
311       return;
312    }
313    strcpy(bp->filepath, gbp->filepath);
314    strcpy(bp->filename, gbp->filename);
315 
316    bp->lw = NULL;
317    bp->ew = NULL;
318    bp->mw = NULL;
319    bp->sortedby = sortedby;
320 
321    /* delete file in memory */
322    if ((status = dbtDeleteTree(gbp->treeIdx)) != DBT_OK) {
323       guwError(status);
324    }
325 
326    if ((status = gueCloseBibErrWin(gbp)) != OK) {
327       guwError(status);
328    }
329 
330    if ((status = gubCloseBibWin(gbp)) != OK) {
331       guwError(status);
332    }
333    gbp = NULL;
334 
335    /* remove confirm box */
336    XtPopdown(shell);
337    XtSetSensitive(mainMenu, TRUE);
338    gubSetSensitive(NULL, TRUE);
339    glbReadFileOpenBib(bp);
340 }
341 
342 
343