A
download save.c
Language: C
License: GPL
Copyright: (C) 1998 Gloria Bueno (C) 1998,1999,2000,2001,2002 Yohann Sulaiman. (C) 2002 Free Software Foundation, Inc.
LOC: 244
Project Info
MultiSlice RTP Environment(yhs)
Server: SourceForge
Type: cvs
...eForge\y\yhs\yhs\root\root\
   app_glob.c
   command_run.c
   command_slice.c
   ctomass.c
   edge.c
   editing_tools.c
   exit.c
   FILE_ID.DIZ
   file_viewer.c
   file2.raw
   filters.c
   glxdino.c
   glxmotif.c
   glxsimple.c
   gpl.html
   icon.h
   initapp.c
   inject.c
   label.c
   log.h
   logmen.h
   logo.h
   main.h
   makefile.32
   makefile.64
   makefile.f32
   measure.c
   menu.c
   multislice.c
   neighbour.c
   open.c
   open.old.c
   paperplane.c
   print.c
   program1.c
   region.c
   region_ops.c
   save.c
   slice.c
   split_merge.c
   textfun.c
   version.c
   viewslice.c
   wals.study
   watershed.c
   xbutton.c
   xmenu.c
   xwins.h
   xwinutil.c
   ys_ext.c
   zoom.c
   zoom2.c

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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
  /*

MultiSlice RTP Environment
(C) 1998,1999,2000,2001,2002 Yohann Sulaiman.
(C) 1998 Gloria Bueno
(C) 2002  Free Software Foundation, Inc. 


    This file is part of the source code of the MultiSlice RTP Environment.

    MultiSlice RTP Environment is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    MultiSlice RTP Environment  is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MultiSlice RTP Environment; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/


/*-------------------------------------------------------------*/
/*                                                             */
/*     File: save.c                                            */
/*                                                             */
/*     C-T Image Application Program                           */
/*                                                             */
/*     OSF/Motif version.                                      */
/*                                                             */
/*-------------------------------------------------------------*/

#include "main.h"

#include <Xm/Xm.h>
#include <Xm/FileSB.h>
#include <Xm/MessageB.h>

#define MAX_FILE_NAME_SIZE   255

/* Extern variables */
extern char *action;
extern int selection;
extern int yhs_files_open;
extern char *yhs_filename1;
extern int file_not_loaded;
extern XImage *theXImage_2;
extern void refresh_action(void);

/* Variables for setting resources */
static Arg args[MAXARGS];
static Cardinal argcount;

/* Local variables */
static Widget file_select      = NULL;
static Widget overwrite_dialog = NULL;
static Widget save_fail_dialog = NULL;
static char filename[MAX_FILE_NAME_SIZE] = "\0";
static char temp_filename[MAX_FILE_NAME_SIZE] = "\0";

/* Function prototypes */
void create_save_as_interface(Widget parent);
void exit_save_as_dialog(void);
void save_as_callback(Widget w, XtPointer client_data,
                      XmAnyCallbackStruct *call_data);

static Widget create_file_select_dialog(Widget parent);
static void activate_file_select_dialog(void);
static void deactivate_file_select_dialog(void);
static void fs_ok_callback(Widget w, XtPointer client_data,
                           XmFileSelectionBoxCallbackStruct *call_data);
static void fs_cancel_callback(Widget w, XtPointer client_data,
                               XmFileSelectionBoxCallbackStruct *call_data);

static void create_overwrite_dialog(Widget parent);
static void activate_overwrite_dialog(void);
static void deactivate_overwrite_dialog(void);
static void overwrite_yes_callback(Widget w, XtPointer client_data,
                                   XmAnyCallbackStruct *call_data);
static void overwrite_no_callback(Widget w, XtPointer client_data,
                                  XmAnyCallbackStruct *call_data);

static void create_save_fail_dialog(Widget parent);
static void activate_save_fail_dialog(void);
static void deactivate_save_fail(void);
static void save_fail_ok_callback(Widget w, XtPointer client_data,
                                  XmAnyCallbackStruct *call_data);

static int save_app_workspace(char *filename);
static int does_file_exist(char *filename);
static void do_save(void);
static char *set_temp_filename(char *new_filename);
static char *get_temp_filename();
static char *set_filename(char *new_filename);
static char *get_filename();

/* Function definition */
void create_save_as_interface(Widget parent)
{
  Widget parent_shell, temp_parent;

  /* Derive the parent shell from the supplied widget */
  parent_shell = parent;
  while ( ! XtIsShell(parent_shell) ) {
    parent_shell = XtParent(parent_shell);
  }

  temp_parent = create_file_select_dialog(parent_shell);

  create_overwrite_dialog(temp_parent);

  create_save_fail_dialog(temp_parent);
}

void exit_save_as_dialog(void)
{
  deactivate_file_select_dialog();
}

void save_as_callback(Widget w, XtPointer client_data,
                      XmAnyCallbackStruct *call_data)
{
  if (yhs_files_open == 0) return;
  selection = SAVE_AS;
  action = SELECT;
  refresh_action();
  activate_file_select_dialog();
}

static Widget create_file_select_dialog(Widget parent)
{
  Widget temp;

  file_select = XmCreateFileSelectionDialog(parent, "file select", NULL, 0);

  temp = XmFileSelectionBoxGetChild(file_select, XmDIALOG_HELP_BUTTON);
  XtUnmanageChild(temp);

  temp = XmFileSelectionBoxGetChild(file_select, XmDIALOG_DIR_LIST);
  XtUnmanageChild(temp);

  temp = XmFileSelectionBoxGetChild(file_select, XmDIALOG_DIR_LIST_LABEL);
  XtUnmanageChild(temp);

  /* Install the callbacks */
  XtAddCallback(file_select, XmNokCallback,
                (XtCallbackProc) fs_ok_callback, (XtPointer) NULL);
  XtAddCallback(file_select, XmNcancelCallback,
                (XtCallbackProc) fs_cancel_callback, (XtPointer) NULL);

  return file_select;
}

static void activate_file_select_dialog(void)
{
  XmString dummy;

  dummy = XmStringCreate("*.raw", XmSTRING_DEFAULT_CHARSET );
  XmFileSelectionDoSearch(file_select, dummy); 
  XmStringFree(dummy);
  XtManageChild(file_select);
}

static void deactivate_file_select_dialog(void)
{
  XtUnmanageChild(file_select);
}

static void fs_ok_callback(Widget w, XtPointer client_data,
                           XmFileSelectionBoxCallbackStruct *call_data)
{
  char *filename;
  XmString x_filename;

  x_filename = call_data->value;
  XmStringGetLtoR(x_filename, XmSTRING_DEFAULT_CHARSET, &filename);

  set_temp_filename(filename);
  XtFree(filename);
  if (does_file_exist(get_temp_filename())) {
    activate_overwrite_dialog();
  } else {
    do_save();
  }
}

static void fs_cancel_callback(Widget w, XtPointer client_data,
                               XmFileSelectionBoxCallbackStruct *call_data)
{
  exit_save_as_dialog();
}

static void create_overwrite_dialog(Widget parent)
{
  Widget temp;

  overwrite_dialog = XmCreateQuestionDialog(parent, "overwrite dialog", NULL, 0);

  temp = XmMessageBoxGetChild(overwrite_dialog, XmDIALOG_HELP_BUTTON);
  XtUnmanageChild(temp);

  /* Install the callbacks */
  XtAddCallback(overwrite_dialog, XmNokCallback, 
                (XtCallbackProc) overwrite_yes_callback, (XtPointer) NULL);
  XtAddCallback(overwrite_dialog, XmNcancelCallback, 
                (XtCallbackProc) overwrite_no_callback, (XtPointer) NULL);
}

static void activate_overwrite_dialog(void)
{
  char       buffer[MAX_BUFFER_SIZE];
  XmString   question;

  strcpy(buffer, "Overwrite the file \n");
  strcat(buffer, get_temp_filename());
  strcat(buffer, " ?");

  question = XmStringCreateLtoR(buffer, XmSTRING_DEFAULT_CHARSET);

  argcount = 0;
  XtSetArg(args[argcount], XmNmessageString, question);  argcount++;
  XtSetValues(overwrite_dialog, args, argcount);

  XmStringFree(question);

  XtManageChild(overwrite_dialog);
}

static void deactivate_overwrite_dialog(void)
{
  /* null - currently autounmanage */
}

static void overwrite_yes_callback(Widget w, XtPointer client_data,
                                   XmAnyCallbackStruct *call_data)
{
  do_save();
}

static void overwrite_no_callback(Widget w, XtPointer client_data,
                                  XmAnyCallbackStruct *call_data)
{
  /* null - currently autounmanage */
}

static void create_save_fail_dialog(Widget parent)
{
  Widget temp;

  save_fail_dialog = XmCreateWarningDialog(parent, "save fail", NULL, 0);

  temp = XmMessageBoxGetChild(save_fail_dialog, XmDIALOG_HELP_BUTTON);
  XtUnmanageChild(temp);
  temp = XmMessageBoxGetChild(save_fail_dialog, XmDIALOG_CANCEL_BUTTON);
  XtUnmanageChild(temp);

  /* Install the callback */
  XtAddCallback(save_fail_dialog, XmNokCallback, 
                (XtCallbackProc) save_fail_ok_callback, (XtPointer) NULL);
}

static void activate_save_fail_dialog(void)
{
  char       buffer[MAX_BUFFER_SIZE];
  XmString   x_message;

  strcpy(buffer, "An error occured while\nwriting the file \n");
  strcat(buffer, get_temp_filename());

  x_message = XmStringCreateLtoR(buffer, XmSTRING_DEFAULT_CHARSET);

  argcount = 0;
  XtSetArg(args[argcount], XmNmessageString, x_message);  argcount++;
  XtSetValues(save_fail_dialog, args, argcount); 

  XmStringFree(x_message);

  XtManageChild(save_fail_dialog);
}

static void deactivate_save_fail(void)
{
  /* null - currently autounmanage */
}

static void save_fail_ok_callback(Widget w, XtPointer client_data,
                                  XmAnyCallbackStruct *call_data)
{
  /* null - currently autounmanage */
}

static int save_app_workspace(char *filename)
{
  FILE *fp;
  FILE *wp;
  int i, j;
  int state;
  if ((wp = fopen(yhs_filename1,"r")) != NULL) {
  if ((fp = fopen(filename,"w")) != NULL) {
    for(i=0; i<IMAGE_WIDTH; i++)
      for(j=0; j<IMAGE_HEIGHT; j++)
        putc((unsigned char) getc(wp), fp);
    fclose(fp);
    fclose(wp);
    state = 1;
  } else {
    state = 0;
  }  
}
  return state;
}

static int does_file_exist(char *filename)
{
  int  it_does;
  FILE *temp_file;

  it_does = ((temp_file = fopen(filename,"r")) != NULL);
  if (it_does) fclose(temp_file);
  return it_does;
}

static void do_save(void)
{
  if (save_app_workspace(get_temp_filename())) {
    /* save was sucessful */
    set_filename(get_temp_filename());
    exit_save_as_dialog();
  } else {
    /* save was unsucessful */
    activate_save_fail_dialog();
  }
}

static char *set_temp_filename(char *new_filename)
{
  strncpy(temp_filename, new_filename, (size_t) MAX_FILE_NAME_SIZE);
  return temp_filename;
}

static char *get_temp_filename()
{
  return temp_filename;
}

static char *set_filename(char *new_filename)
{
  strncpy(filename, new_filename, (size_t) MAX_FILE_NAME_SIZE);
  return filename;
}

static char *get_filename()
{
  return filename;
}

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us