File: ctl_open.c

package info (click to toggle)
bibview 2.2-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,252 kB
  • ctags: 1,588
  • sloc: ansic: 15,149; yacc: 1,145; makefile: 297; lex: 221; sh: 17
file content (342 lines) | stat: -rw-r--r-- 10,871 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*********************************************************************/
/*  bibView: Administration of BibTeX-Databases                      */
/*           (Verwaltung von BibTeX-Literaturdatenbanken)            */
/*                                                                   */
/*  Module:  ctl_open.c                                              */
/*                                                                   */
/*             Open Control                                          */
/*             - Menu function New                                   */
/*             - Menu function Open                                  */
/*                                                                   */
/*  Author:  Holger Martin,  martinh@informatik.tu-muenchen.de       */
/*           Peter M. Urban, urban@informatik.tu-muenchen.de         */
/*                                                                   */
/*  History:                                                         */
/*    12.05.91  PMU  created                                         */
/*    05.26.92       Version 1.0 released                            */
/*                                                                   */
/*  Copyright 1992 TU MUENCHEN                                       */
/*    See ./Copyright for complete rights and liability information. */
/*                                                                   */
/*********************************************************************/

#include <stdio.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xaw/Cardinals.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Dialog.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/Box.h>
#include "FileNom.h"
#include "bibview.h"


/* macros and definitions */
/* ---------------------- */
#define NN_FILENAME	"noname"
#define NN_FILEEXT	".bib"
#define NN_FILENAMELEN  14


/* imported global variables */
/* ------------------------- */
extern XtAppContext app_context;
extern Widget topLevel, mainMenu, desktop;
extern Pixmap questPixmap;

extern int sortedby;

extern char *actual_path;


/* exported global variables */
/* ------------------------- */


/* local function prototypes */
/* ------------------------- */
static void LoadBibFile (Widget w, XtPointer clientData, XtPointer callData);
static void CancelLoad (Widget w, XtPointer clientData, XtPointer callData);
static void confirmLoad (BibPtr bp);
static void cancelLoadCmd (Widget w, XtPointer clientData, XtPointer callData);
static void loadCmdOk (Widget w, XtPointer clientData, XtPointer callData);


/* local global variables */
/* ---------------------- */
static int nnCnt = 0;
static BibPtr gbp = NULL;


/*********************************************************************/
/* copNewCmd:                                                        */
/*    Callback function for option new in main window file menu      */
/*********************************************************************/
void
copNewCmd (Widget w, XtPointer clientData, XtPointer callData)
{
BibPtr bp;
FILE *tempFile;
int status;
char *tempName;

   /* make new global data struct for bib file */
   if ((status = glbNewBibListEl(&bp)) != OK) {
      guwError(status);
      return;
   }

   /* build new noname filename */
   sprintf(bp->filename, "%s%d%s", NN_FILENAME, nnCnt+1, NN_FILEEXT);
   /*
#ifdef SYSV
   (void) getcwd(bp->filepath, MAX_FILEPATHLEN);
#else 
   (void) getwd(bp->filepath, MAX_FILEPATHLEN);
#endif
   */
   strcpy(bp->filepath, actual_path);
   strcat(bp->filepath, "/");
   strcat(bp->filepath, bp->filename);

   bp->ew = NULL;
   bp->mw = NULL;
   bp->lw = NULL;
   bp->sortedby = sortedby;

   /* create temp file */
#ifdef NO_TEMPNAM
   tempName = (char *)tmpnam(NULL);
   bp->tempfile = (char *)XtMalloc(strlen(tempName)+1);
   strcpy(bp->tempfile,tempName);
#else
   bp->tempfile = (char *)tempnam(NULL, NULL);
#endif
   tempFile = fopen(bp->tempfile, "w" );
   fclose(tempFile);

   /* display window for bib */
   if ((status = gubOpenBibWin(bp)) != OK) {
      guwError(status); 
      return;
   }
   nnCnt++;
}


/*********************************************************************/
/* copOpenCmd:                                                       */
/*    Callback function for option open in main window file menu     */
/*********************************************************************/
void
copOpenCmd (Widget w, XtPointer clientData, XtPointer callData)
{
static Widget fsbShell, fsbBox, fsbDialog;
Position dx, dy, x, y;

   XtVaGetValues(desktop,
                 XtNx, &dx,
                 XtNy, &dy, NULL);
   XtTranslateCoords(desktop,
                     (Position)dx + SUBWIN_MARGIN,
                     (Position)dy + SUBWIN_MARGIN,
                     &x, &y);
   fsbShell  = XtVaCreatePopupShell("fileSelectBoxShell",
	         topLevelShellWidgetClass, desktop, 
	 	 XtNx, x, XtNy, y, NULL);
   fsbBox    = XtVaCreateManagedWidget("fileSelectBox",
                 boxWidgetClass, fsbShell, NULL);
   fsbDialog = XtVaCreateManagedWidget("loadFileBoxShell",
                fileNominatorWidgetClass, fsbBox,
                XtNborderWidth, 0, NULL);

   XtAddCallback(fsbDialog, XtNcancelCallback, CancelLoad, fsbShell);
   XtAddCallback(fsbDialog, XtNselectCallback, LoadBibFile, fsbDialog);

   XtSetSensitive(mainMenu, FALSE);
   gubSetSensitive(NULL, FALSE);
   XtPopup(fsbShell, XtGrabNonexclusive);
}



/*********************************************************************/
/* LOCAL FUNCTIONS                                                   */
/*********************************************************************/

/*********************************************************************/
/* CancelLoad:                                                       */
/*    Callback function for CANCEL button in file select box         */
/*********************************************************************/
static void
CancelLoad (Widget w, XtPointer clientData, XtPointer callData)
{
Widget dialog = (Widget)clientData;

   XtSetSensitive(mainMenu, TRUE);
   gubSetSensitive(NULL, TRUE);
   XtPopdown(dialog);

}


/*********************************************************************/
/* LoadBibFile:                                                      */
/*    Callback function for OK button in file select box             */
/*********************************************************************/
static void
LoadBibFile (Widget w, XtPointer clientData, XtPointer callData)
{
Widget dialog = (Widget)clientData;
String filepath;
BibPtr p, bp;
Errcode status;

   /* get and keep filename */
   filepath = (String)FileNominatorGetFullFileName(dialog);
   if (filepath == NULL){
      XtPopdown(XtParent(XtParent(dialog)));
      guwError(NO_VALID_FILENAME);
      return;
      }

   /* check if opened already */
   p = glbFirstBibListEl();
   while (p != NULL) {
      if (strcmp(p->filepath, filepath) == 0) {
	 XtSetSensitive(mainMenu, TRUE);
	 gubSetSensitive(NULL, TRUE);
	 XtPopdown(XtParent(XtParent(dialog)));
	 confirmLoad(p);
	 return;
      }
      p = glbNextBibListEl(p);
   }

   /* make new global data struct for bib file */
   if ((status = glbNewBibListEl(&bp)) != OK) {
      guwError(status);
      XtSetSensitive(mainMenu, TRUE);
      gubSetSensitive(NULL, TRUE);
      XtPopdown(XtParent(XtParent(dialog)));
      return;
   }
   strcpy(bp->filepath, filepath);
   strcpy(bp->filename, (String)FileNominatorGetFileName(dialog));
   bp->mw=NULL;
   bp->ew=NULL;
   bp->lw=NULL;

   bp->sortedby = sortedby;

   /* remove file select box */
   XtSetSensitive(mainMenu, TRUE);
   gubSetSensitive(NULL, TRUE);
   XtPopdown(XtParent(XtParent(dialog))); 

   glbReadFileOpenBib(bp);

}


/*********************************************************************/
/* confirmLoad:                                                      */
/*    Opens dialogbox for user to confirm loading from disk          */
/*********************************************************************/
static void
confirmLoad (BibPtr bp)
{
static Widget conShell, conDialog, conOKButton, conQuitButton;
Position dx, dy, x, y;

   XtVaGetValues(desktop,
                 XtNx, &dx,
                 XtNy, &dy, NULL);
   XtTranslateCoords(desktop,
                     (Position)dx + SUBWIN_MARGIN,
                     (Position)dy + SUBWIN_MARGIN,
                     &x, &y);
   conShell = XtVaCreatePopupShell("confirmLoadShell",
	        transientShellWidgetClass, topLevel, 
		XtNx, x, XtNy, y, NULL);
   conDialog = XtVaCreateManagedWidget("confirmLoadBox",
	         dialogWidgetClass, conShell, 
		 XtNicon, questPixmap, NULL);
   conOKButton = XtVaCreateManagedWidget("ok",
	           commandWidgetClass, conDialog, NULL);
   conQuitButton = XtVaCreateManagedWidget("cancel",
	           commandWidgetClass, conDialog, NULL);
   XtAddCallback(conQuitButton, XtNcallback, cancelLoadCmd, conShell);
   XtAddCallback(conOKButton, XtNcallback, loadCmdOk, conShell);

   XtSetSensitive(mainMenu, FALSE);
   gubSetSensitive(NULL, FALSE);
   gbp = bp;
   XtPopup(conShell, XtGrabNonexclusive);
}


/*********************************************************************/
/* cancelLoadCmd:                                                    */
/*    Callback function for CANCEL button in confirm box             */
/*********************************************************************/
static void
cancelLoadCmd (Widget w, XtPointer clientData, XtPointer callData)
{
Widget shell = (Widget)clientData;

   XtSetSensitive(mainMenu, TRUE);
   gubSetSensitive(NULL, TRUE);
   XtPopdown(shell);
   gbp = NULL;
}


/*********************************************************************/
/* loadCmdOK:                                                        */
/*    Callback function for OK button in confirm box                 */
/*********************************************************************/
static void
loadCmdOk (Widget w, XtPointer clientData, XtPointer callData)
{
Widget shell = (Widget) clientData;
BibPtr bp;
Errcode status;

   /* make new global data struct for bib file */
   if ((status = glbNewBibListEl(&bp)) != OK) {
      guwError(status);
      return;
   }
   strcpy(bp->filepath, gbp->filepath);
   strcpy(bp->filename, gbp->filename);
   
   bp->lw = NULL;
   bp->ew = NULL;
   bp->mw = NULL;
   bp->sortedby = sortedby;

   /* delete file in memory */
   if ((status = dbtDeleteTree(gbp->treeIdx)) != DBT_OK) {
      guwError(status);
   }

   if ((status = gueCloseBibErrWin(gbp)) != OK) {
      guwError(status);
   }

   if ((status = gubCloseBibWin(gbp)) != OK) {
      guwError(status);
   }
   gbp = NULL;

   /* remove confirm box */
   XtPopdown(shell);
   XtSetSensitive(mainMenu, TRUE);
   gubSetSensitive(NULL, TRUE);
   glbReadFileOpenBib(bp);
}