chiark / gitweb /
Initial revision
[ssr] / StraySrc / Libraries / Steel / c / saveas
1 /*
2  * saveas
3  *  Handler for a save dbox under new dbox system
4  *
5  * v. 1.00 (9 August 1993)
6  *
7  * © 1993-1998 Straylight
8  */
9
10 /*----- Licensing note ----------------------------------------------------*
11  *
12  * This file is part of Straylight's Steel library.
13  *
14  * Steel is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2, or (at your option)
17  * any later version.
18  *
19  * Steel is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with Steel.  If not, write to the Free Software Foundation,
26  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 #include "wimp.h"
30 #include "wimpt.h"
31 #include "win.h"
32 #include "dbox.h"
33 #include "help.h"
34 #include "saveas.h"
35 #include "fileicon.h"
36 #include "stddbox.h"
37 #include "msgs.h"
38 #include "template.h"
39 #include "werr.h"
40
41 static dbox saveas__db;
42 static int saveas__filetype;
43 static xfersend_saveproc saveas__saveproc;
44 static xfersend_sendproc saveas__sendproc;
45 static xfersend_printproc saveas__printproc;
46 static void *saveas__handle;
47 static int saveas__estsize;
48 static char *saveas__title;
49 static BOOL saveas__xfer;
50
51 #define saveas__FILEICON 1
52 #define saveas__FILENAME 2
53 #define saveas__OK 0
54
55 /*
56  * BOOL saveas__raw(dbox d,wimp_eventstr *e,void *handle)
57  *
58  * Use
59  *  Raw event handler for the save as dialogue box.
60  *
61  * Parameters
62  *  dbox d == the saveas dialogue box
63  *  wimp_eventstr *e == the current wimp event
64  *  void *handle == the caller's handle
65  */
66
67 static BOOL saveas__raw(dbox d,wimp_eventstr *e,void *handle)
68 {
69   BOOL handled=FALSE;
70   char name[256];
71   switch (e->e)
72   {
73     case wimp_EBUT:
74       if (e->data.but.m.bbits==wimp_BDRAGLEFT)
75       {
76         dbox_getfield(d,saveas__FILENAME,name,256);
77         saveas__xfer=TRUE;
78         xfersend
79         (
80           saveas__filetype,
81           name,
82           saveas__estsize,
83           saveas__saveproc,
84           saveas__sendproc,
85           saveas__printproc,
86           e,
87           handle
88         );
89         handled=TRUE;
90       }
91       break;
92   }
93   return (handled);
94 }
95
96 /*
97  * void saveas
98  * (
99  *   char *title,
100  *   char *name,
101  *   int filetype,
102  *   int estsize,
103  *   xfersend_saveproc saveproc,
104  *   xfersend_sendproc sendproc,
105  *   xfersend_printproc printproc,
106  *   void *handle
107  * )
108  *
109  * Use
110  *  Creates and handles a save as dialogue box (even saving your data for
111  *  you!).
112  *
113  * Parameters
114  *  char *title == the title of the dialogue box.
115  *  char *name == the default filename for the box.
116  *  int filetype == the filetype of the data to be sent.
117  *  int estsize == the estimated file size.
118  *  xfersend_saveproc saveproc == function to save the data.
119  *  xfersend_sendproc sendproc == function to export data to another
120  *    application (RAM transfer).
121  *  xfersend_printproc printproc == function to print data.
122  *  void *handle == your handle to the data (or anything else!)
123  */
124
125 void saveas
126 (
127   char *title,
128   char *name,
129   int filetype,
130   int estsize,
131   xfersend_saveproc saveproc,
132   xfersend_sendproc sendproc,
133   xfersend_printproc printproc,
134   void *handle
135 )
136 {
137   BOOL stop=FALSE;
138   int index;
139   BOOL dotYet;
140   char fname[256];
141   saveas__title=title;
142   saveas__filetype=filetype;
143   saveas__estsize=estsize;
144   saveas__saveproc=saveproc;
145   saveas__sendproc=sendproc;
146   saveas__printproc=printproc;
147   saveas__handle=handle;
148   if (template_exists("save"))
149   {
150     if (saveas__db=dbox_create("save"),saveas__db==0)
151       return;
152   }
153   else
154   {
155     if (saveas__db=dbox_create("xfer_send"),saveas__db==0)
156       return;
157   }
158   xfersend_close_on_xfer(TRUE,dbox_syshandle(saveas__db));
159   win_settitle(dbox_syshandle(saveas__db),"%s",saveas__title);
160   dbox_setfield(saveas__db,saveas__FILENAME,"%s",name);
161   fileicon(dbox_syshandle(saveas__db),
162            saveas__FILEICON,
163            saveas__filetype,
164            name);
165   dbox_rawEventHandler(saveas__db,saveas__raw,saveas__handle);
166   dbox_display(saveas__db,FALSE);
167   while (!stop)
168   {
169     switch (dbox_fillin(saveas__db))
170     {
171       case dbox_CLOSE:
172         stop=TRUE;
173         break;
174       case dbox_HELP:
175         if (help_wasHelp())
176         {
177           help_startHelp();
178           help_addLine
179           (
180             msgs_lookup("saveasHM:This is the %s dialogue box."),
181             saveas__title
182           );
183           switch (dbox_helpField())
184           {
185             case saveas__FILEICON:
186               help_addLine(msgs_lookup
187               (
188                 "saveasFIH:Drag this icon to a Filer window or "
189                                    "another application to save the file."
190               ));
191               break;
192           }
193           help_readFromIcon();
194           help_endHelp();
195         }
196         break;
197       case saveas__OK:
198         if (wimpt_options() & 7)
199           dbox_clickicon(saveas__db,saveas__OK);
200         dbox_getfield(saveas__db,saveas__FILENAME,fname,256);
201         dotYet=FALSE;
202         for (index=0;fname[index]!=0;index++)
203         {
204           if (fname[index]=='.')
205             dotYet=TRUE;
206         }
207         if (!dotYet)
208         {
209           if (wimpt_options() & 7)
210           {
211             note(msgs_lookup("saveasDI:To save, drag icon to a "
212                                                     "directory viewer."));
213             dbox_unclick();
214           }
215           else
216           {
217             werr(FALSE,msgs_lookup("saveasDI:To save, drag icon to a "
218                                                     "directory viewer."));
219           }
220         }
221         else
222         {
223           saveas__xfer=FALSE;
224           stop=saveas__saveproc(fname,saveas__handle);
225           if (dbox_wasAdjustClick() || !stop)
226           {
227             if (wimpt_options() & 7)
228               dbox_unclick();
229             stop=FALSE;
230           }
231           else
232           {
233             dbox_hide(saveas__db);
234             if (wimpt_options() & 7)
235               dbox_unclick();
236           }
237         }
238         break;
239     }
240   }
241   xfersend_close_on_xfer(FALSE,0);
242   dbox_delete(saveas__db);
243 }
244
245 /*
246  * BOOL saveas_file_is_safe(void)
247  *
248  * Use
249  *  Informs caller if the file is going to a safe home.
250  *
251  * Returns
252  *  TRUE if the file is 'safe' - i.e. on disk
253  */
254
255 BOOL saveas_file_is_safe(void)
256 {
257   if (saveas__xfer)
258     return (xfersend_file_is_safe());
259   else
260    return (TRUE);
261 }