1 // $Id: xxFilename.cc 4497 2012-05-27 00:33:09Z flaterco $
2 
3 /*  xxFilename  Get a file name and format from the user.  If successful, do
4     caller.save (filename, format).
5 
6     Copyright (C) 1998  David Flater.
7 
8     This program is free software: you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License
19     along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "xtide.hh"
23 #include "xxHorizDialog.hh"
24 #include "xxMultiChoice.hh"
25 #include "xxReconfigurable.hh"
26 #include "xxFilename.hh"
27 
28 
xxFilenameCallback(Widget w unusedParameter,XtPointer client_data,XtPointer call_data unusedParameter)29 static void xxFilenameCallback (Widget w unusedParameter,
30 				XtPointer client_data,
31 				XtPointer call_data unusedParameter) {
32   assert (client_data);
33   xxFilename *prompts ((xxFilename *)client_data);
34   prompts->callback();
35   delete prompts->dismiss();
36 }
37 
38 
39 
xxFilenameCancelCallback(Widget w unusedParameter,XtPointer client_data,XtPointer call_data unusedParameter)40 static void xxFilenameCancelCallback (Widget w unusedParameter,
41 				      XtPointer client_data,
42 				      XtPointer call_data unusedParameter) {
43   assert (client_data);
44   delete ((xxFilename *)client_data)->dismiss();
45 }
46 
47 
callback()48 void xxFilename::callback() {
49   Dstr filename (filenameDiag->val());
50   if (filename.strchr ('\n') != -1 ||
51       filename.strchr ('\r') != -1 ||
52       filename.strchr (' ') != -1 ||
53       filename[0] == '-') {
54     Dstr details ("Well, it's not that I can't do it, it's that you probably\n\
55 don't want me to.  The filename that you entered is '");
56     details += filename;
57     details += "'.\n";
58     if (filename[0] == '-')
59       details += "Filenames that begin with a dash are considered harmful.";
60     else
61       details += "Whitespace in filenames is considered harmful.";
62     Global::barf (Error::CANT_OPEN_FILE, details, Error::nonfatal);
63   } else {
64     Format::Format form (Format::text);
65     if (!_textOnly)
66       switch (formatChoice->choice()) {
67       case 0:
68         form = Format::PNG;
69         break;
70       case 1:
71         form = Format::SVG;
72         break;
73       default:
74         assert (false);
75       }
76     _caller.save (filename, form);
77   }
78 }
79 
80 
reconfigure()81 void xxFilename::reconfigure() {
82   if (!_textOnly) {
83     Dstr filename (filenameDiag->val());
84     if (filename.length() > 3) {
85       unsigned i (filename.length()-4);
86       Dstr extension (filename.ascharfrom(i));
87       switch (formatChoice->choice()) {
88       case 0:
89         if (extension.contains (".svg")) {
90           filename -= i;
91           filename += ".png";
92 	  filenameDiag->val (filename);
93         }
94 	break;
95       case 1:
96         if (extension.contains (".png")) {
97           filename -= i;
98           filename += ".svg";
99   	  filenameDiag->val (filename);
100         }
101 	break;
102       default:
103 	assert (false);
104       }
105     }
106   }
107 }
108 
109 
~xxFilename()110 xxFilename::~xxFilename() {
111   unrealize();
112   _caller.noClose = false;
113 }
114 
115 
xxFilename(const xxWidget & parent,xxPredictionWindow & caller,bool textOnly)116 xxFilename::xxFilename (const xxWidget &parent,
117 			xxPredictionWindow &caller,
118 			bool textOnly):
119   xxWindow (parent, boxContainer, XtGrabExclusive),
120   _caller(caller),
121   _textOnly(textOnly) {
122 
123   _caller.noClose = true;
124   setTitle ("Save prompts");
125 
126   Arg buttonArgs[4] =  {
127     {XtNvisual, (XtArgVal)xxX::visual},
128     {XtNcolormap, (XtArgVal)xxX::colormap},
129     {XtNbackground, (XtArgVal)xxX::pixels[Colors::button]},
130     {XtNforeground, (XtArgVal)xxX::pixels[Colors::foreground]}
131   };
132 
133   static constString textChoices[] = {"txt", NULL};
134   static constString graphChoices[] = {"png", "svg", NULL};
135   formatChoice = std::auto_ptr<xxMultiChoice> (new xxMultiChoice (*container,
136 								  "Format:",
137 				       (textOnly ? textChoices : graphChoices),
138 								  0,
139 								  *this));
140 
141   constString initFilename = (textOnly ? "tides.txt" : "tides.png");
142   filenameDiag = std::auto_ptr<xxHorizDialog> (new xxHorizDialog (
143                                                                 *container,
144 							        "Filename:",
145 							        initFilename));
146   {
147     Widget buttonWidget = xxX::createXtWidget ("Go", commandWidgetClass,
148       container->widget(), buttonArgs, 4);
149     XtAddCallback (buttonWidget, XtNcallback, xxFilenameCallback,
150       (XtPointer)this);
151     goButton = xxX::wrap (buttonWidget);
152   }{
153     Widget buttonWidget = xxX::createXtWidget ("Cancel", commandWidgetClass,
154       container->widget(), buttonArgs, 4);
155     XtAddCallback (buttonWidget, XtNcallback, xxFilenameCancelCallback,
156       (XtPointer)this);
157     cancelButton = xxX::wrap (buttonWidget);
158   }
159 
160   realize();
161 }
162 
163 // Cleanup2006 Done
164