chiark / gitweb /
Initial revision
[ssr] / StraySrc / Libraries / Steel / c / stddbox
1 /*
2  * stddbox
3  *
4  * Some standard Straylight dboxes.
5  *
6  * © 1993-1998 Straylight
7  */
8
9 /*----- Licensing note ----------------------------------------------------*
10  *
11  * This file is part of Straylight's Steel library.
12  *
13  * Steel is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2, or (at your option)
16  * any later version.
17  *
18  * Steel is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with Steel.  If not, write to the Free Software Foundation,
25  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26  */
27
28 #include "dbox.h"
29 #include "nopoll.h"
30 #include "bbc.h"
31 #include "stddbox.h"
32 #include "werr.h"
33 #include "win.h"
34 #include "wimpt.h"
35 #include "msgs.h"
36 #include "saveas.h"
37 #include "help.h"
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <string.h>
41
42 #define stddbox__WARNING_TITLE 0
43 #define stddbox__WARNING_MESSAGE 5
44 #define stddbox__WARNING_OK 4
45 #define stddbox__WARNING_CANCEL 3
46
47 #define stddbox__NOTE_TITLE 2
48 #define stddbox__NOTE_MESSAGE 0
49 #define stddbox__NOTE_OK 1
50
51 #define stddbox__WRITE_OK 0
52 #define stddbox__WRITE_WRITE 1
53
54 #define stddbox__SAVEWARN_TITLE 0
55 #define stddbox__SAVEWARN_MESSAGE 5
56 #define stddbox__SAVEWARN_CANCEL 3
57 #define stddbox__SAVEWARN_SAVE 4
58 #define stddbox__SAVEWARN_REMOVE 7
59
60 #define stddbox__PROGINFO_TITLE 0
61 #define stddbox__PROGINFO_NAME 7
62 #define stddbox__PROGINFO_PURPOSE 5
63 #define stddbox__PROGINFO_AUTHOR 3
64 #define stddbox__PROGINFO_VERSION 1
65
66 static xfersend_saveproc stddbox__save;
67 static BOOL stddbox__saved;
68
69 /*
70  * BOOL warning(char *okMsg,char *warn,...)
71  *
72  * Use
73  *  Pops up a warning box, with a Cancel button and a default action button
74  *  with your own text in it.
75  *
76  * Parameters
77  *  char *okMsg == what to put in the default action button
78  *  char *warn == the warning message (printf()-style format string)
79  *
80  * Returns
81  *  TRUE if the user chose the OK button.
82  */
83
84 BOOL warning(char *okMsg,char *warn,...)
85 {
86   dbox warndb=dbox_create("warning");
87   va_list ap;
88   int clicked;
89   char msg[256];
90   va_start(ap,warn);
91   vsprintf(msg,warn,ap);
92   va_end(ap);
93   if (!warndb)
94     return FALSE;
95   win_settitle
96   (
97     dbox_syshandle(warndb),
98     msgs_lookup("stddbWARN:Warning from %s"),
99     wimpt_programname()
100   );
101   dbox_setfield(warndb,stddbox__WARNING_MESSAGE,msg);
102   dbox_setfield(warndb,stddbox__WARNING_OK,okMsg);
103   if (!dbox_hasTitle(warndb))
104     dbox_setEmbeddedTitle(warndb,stddbox__WARNING_TITLE,FALSE);
105   werr_bleepy();
106   clicked=nopoll_doDbox
107   (
108     warndb,
109     nopoll_CENTRE,
110     stddbox__WARNING_OK,
111     stddbox__WARNING_CANCEL,
112     -1
113   );
114   dbox_delete(warndb);
115   return (clicked==nopoll_OK);
116 }
117
118 /*
119  * void note(char *notemsg,...)
120  *
121  * Use
122  *  Displays a small note on the screen, so you can tell the user that he
123  *  has done something silly, etc.  A lot nicer than old werr().
124  *
125  * Parameters
126  *  char *notemsg == the note (printf()-style format string)
127  */
128
129 void note(char *notemsg,...)
130 {
131   dbox notedb=dbox_create("note");
132   va_list ap;
133   char msg[256];
134   va_start(ap,notemsg);
135   vsprintf(msg,notemsg,ap);
136   va_end(ap);
137   if (!notedb)
138     return;
139   win_settitle
140   (
141     dbox_syshandle(notedb),
142     msgs_lookup("stddbNOTE:Note from %s"),
143     wimpt_programname()
144   );
145   dbox_setfield(notedb,stddbox__NOTE_MESSAGE,msg);
146   if (!dbox_hasTitle(notedb))
147     dbox_setEmbeddedTitle(notedb,stddbox__NOTE_TITLE,FALSE);
148   nopoll_doDbox(notedb,nopoll_ONPTR,stddbox__NOTE_OK,-1,-1);
149   dbox_delete(notedb);
150 }
151
152 /*
153  * BOOL writable
154  * (
155  *   char *title,
156  *   char *deflt,
157  *   char *result,
158  *   stddbox_writableHandler proc,
159  *   void *handle
160  * )
161  *
162  * Use
163  *  Opens a dialogue box for the user to input a string.  Needs the
164  *  'writable' template.
165  *
166  * Parameters
167  *  char *title == the title for the dialogue box.
168  *  char *default == the default string to put in the writable area.
169  *  char *result == a buffer to contain the result string.  May be 0.
170  *  stddbox_writableHandler proc == proc to call when OK is clicked.  May
171  *    be 0.  Return TRUE if successful (i.e. we may close the dbox).
172  *  void *handle == passed to proc.
173  *
174  * Returns
175  *  TRUE if the string has been updated.
176  */
177
178 BOOL writable
179 (
180   char *title,
181   char *deflt,
182   char *result,
183   stddbox_writableHandler proc,
184   void *handle
185 )
186 {
187   dbox w=dbox_create("writable");
188   BOOL stop=FALSE;
189   BOOL changed=FALSE;
190   char buffer[256];
191   BOOL mayClose;
192   if (result==0)
193     result=buffer;
194   if (w==0)
195     return (FALSE);
196   dbox_setfield(w,1,"%s",deflt);
197   win_settitle(dbox_syshandle(w),"%s",title);
198   dbox_display(w,FALSE);
199   while (!stop)
200   {
201     switch (dbox_fillin(w))
202     {
203       case dbox_CLOSE:
204         stop=TRUE;
205         break;
206       case 0:
207         dbox_clickicon(w,0);
208         dbox_getfield(w,1,result,256);
209         if (proc)
210           mayClose=proc(result,handle);
211         else
212           mayClose=TRUE;
213         changed=TRUE;
214         if (dbox_wasAdjustClick() || mayClose==FALSE)
215           dbox_unclick();
216         else
217         {
218           dbox_hide(w);
219           dbox_unclick();
220           stop=TRUE;
221         }
222         break;
223     }
224   }
225   dbox_delete(w);
226   return (changed);
227 }
228
229 /*
230  * BOOL stddbox__saver(char *filename,void *handle)
231  *
232  * Use
233  *  Passes on the save request to the caller's own save routine, and erases
234  *  the data if the file is now safe.
235  *
236  * Parameters
237  *  char *filename == where to save the file
238  *  void *handle == the caller's handle to the data
239  */
240
241 static BOOL stddbox__saver(char *filename,void *handle)
242 {
243   if (stddbox__save(filename,handle)==FALSE)
244     return (FALSE);
245   if (saveas_file_is_safe())
246     stddbox__saved=TRUE;
247   return (TRUE);
248 }
249
250 /*
251  * void saveWarn
252  * (
253  *   BOOL useName,
254  *   void (*dispose)(void *handle),
255  *   char *title,
256  *   char *name,
257  *   int filetype,
258  *   int estsize,
259  *   xfersend_saveproc saveproc,
260  *   xfersend_sendproc sendproc,
261  *   xfersend_printproc printproc,
262  *   void *handle
263  * )
264  *
265  * Use
266  *  Pops up a save warning box allowing the use the luxury of saving his
267  *  data before closing the file.  The file is only removed if the data is
268  *  'safe'.
269  *
270  * Parameters
271  *  BOOL useName == whether to use the given name in the warning message
272  *  void (*dispose)(void *handle) == function to dispose the user's data
273  *  the others == as for saveas()
274  */
275
276 void saveWarn
277 (
278   BOOL useName,
279   void (*dispose)(void *handle),
280   char *title,
281   char *name,
282   int filetype,
283   int estsize,
284   xfersend_saveproc saveproc,
285   xfersend_sendproc sendproc,
286   xfersend_printproc printproc,
287   void *handle
288 )
289 {
290   dbox d;
291   if (win_anyWindows())
292   {
293     d=dbox_create("saveWarn");
294     if (!d)
295       return;
296     win_settitle
297     (
298       dbox_syshandle(d),
299       msgs_lookup("stddbWARN:Warning from %s"),
300       wimpt_programname()
301     );
302     if (useName)
303       dbox_setfield
304       (
305         d,
306         stddbox__SAVEWARN_MESSAGE,
307         msgs_lookup("stddbREFN:File '%s' has been modified, but not saved.  "
308                     "Do you want to discard these changes, save and then "
309                     "close the file, or cancel?"),
310         name
311       );
312     else
313       dbox_setfield
314       (
315         d,
316         stddbox__SAVEWARN_MESSAGE,
317         msgs_lookup("stddbREF:This file has been modified, but not saved.  "
318                     "Do you want to discard these changes, save and then "
319                     "close the file, or cancel?")
320       );
321     if (!dbox_hasTitle(d))
322       dbox_setEmbeddedTitle(d,stddbox__SAVEWARN_TITLE,FALSE);
323     stddbox__save=saveproc;
324     werr_bleepy();
325     switch (nopoll_doDbox
326     (
327       d,
328       nopoll_CENTRE,
329       stddbox__SAVEWARN_SAVE,
330       stddbox__SAVEWARN_CANCEL,
331       stddbox__SAVEWARN_REMOVE
332     ))
333     {
334       case nopoll_OK:
335         stddbox__saved=FALSE;
336         saveas
337         (
338           title,
339           name,
340           filetype,
341           estsize,
342           stddbox__saver,
343           sendproc,
344           printproc,
345           handle
346         );
347         if (stddbox__saved)
348           dispose(handle);
349         break;
350       case nopoll_CANCEL:
351         break;
352       case nopoll_OTHER:
353         dispose(handle);
354         break;
355     }
356     dbox_delete(d);
357   }
358   else
359   {
360     BOOL killIt;
361     if (useName)
362       killIt=werr_error
363       (
364         2,
365         msgs_lookup("stddbREFN:Are you sure you want to "
366                                              "remove edited file '%s'?"),
367         name
368       );
369     else
370       killIt=werr_error
371       (
372         2,
373         msgs_lookup("stddbREF:Are you sure you want to "
374                                               "remove your edited file?")
375       );
376     if (killIt)
377       dispose(handle);
378   }
379 }
380
381 /*
382  * void progInfo
383  * (
384  *   char *name,
385  *   char *purpose,
386  *   char *author,
387  *   int version,
388  *   char *date
389  * )
390  *
391  * Use
392  *  Presents a standard progInfo window giving information about an
393  *  application.
394  *
395  * Parameters
396  *  char *name == the name of the program
397  *  char *purpose == what it does
398  *  char *author == author/copyright string (usually something like
399  *    '© 1993-1998 Straylight')
400  *  int version == the version number*100 (e.g. 374 for 3.74 etc.)
401  *  char *date == the date of compilation (use _TIME_NOW)
402  */
403
404 void progInfo(char *name,char *purpose,char *author,int version,char *date)
405 {
406   dbox d=dbox_create("progInfo");
407   char buffer[100];
408   if (!d)
409     return;
410   if (!dbox_hasTitle(d))
411     dbox_setEmbeddedTitle(d,stddbox__PROGINFO_TITLE,TRUE);
412   dbox_setfield(d,stddbox__PROGINFO_NAME,"%s",name);
413   dbox_setfield(d,stddbox__PROGINFO_PURPOSE,"%s",purpose);
414   dbox_setfield(d,stddbox__PROGINFO_AUTHOR,"%s",author);
415   dbox_setfield
416   (
417     d,
418     stddbox__PROGINFO_VERSION,
419     "%i.%02i (%s)",
420     version/100,
421     version%100,
422     date
423   );
424   sprintf(buffer,
425           msgs_lookup("stddbPIH:This window gives you "
426                       "information about this version of %s."),
427           wimpt_programname());
428   mbox(d,buffer);
429 }
430
431 /*
432  * void mbox(dbox d)
433  *
434  * Use
435  *  Handles a monologue box (like info windows) where no input is required.
436  *  You should create the dbox, fill in any fields required.  This routine
437  *  then handles the rest.  It deletes the dbox when it's finished - it's of
438  *  no use to the caller anyway - who wants a used dialogue box with no
439  *  input?  Yuk...
440  *
441  *  You can specify a help message tag to be displayed before any messages
442  *  embedded in the icons.  This is passed through msgs_lookup before
443  *  sending to help_addLine.  Specify zero for this to send no message.
444  *
445  * Parameters
446  *  dbox d == the box to handle
447  *  char *help == the help message tag to stick on the top
448  */
449
450 void mbox(dbox d,char *help)
451 {
452   BOOL done=FALSE;
453   dbox_display(d,dbox_MENU_OVERPTR);
454   while (!done)
455   {
456     switch (dbox_fillin(d))
457     {
458       case dbox_CLOSE:
459         done=TRUE;
460         break;
461       case dbox_HELP:
462         help_startHelp();
463         if (help)
464           help_addLine(msgs_lookup(help));
465         help_readFromIcon();
466         help_endHelp();
467         break;
468     }
469   }
470   dbox_delete(d);
471 }