]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/xforms_helpers.C
acbc4ce6d00244690b36d1e23e1f46af6ed57248
[lyx.git] / src / frontends / xforms / xforms_helpers.C
1 /** Collection of some useful xform helper functions
2  */
3
4 #include <config.h>
5
6 #include FORMS_H_LOCATION
7
8 #include <fstream> // ofstream
9 #include <vector>
10
11 #ifdef __GNUG_
12 #pragma implementation
13 #endif
14  
15 #include "xforms_helpers.h"
16 #include "lyxlex.h"
17 #include "support/FileInfo.h"
18 #include "support/filetools.h"
19 #include "gettext.h"
20
21 using std::ofstream;
22 using std::pair;
23 using std::vector;
24
25 // Set an FL_OBJECT to activated or deactivated
26 void setEnabled(FL_OBJECT * ob, bool enable)
27 {
28         if (enable) {
29                 fl_activate_object(ob);
30                 fl_set_object_lcol(ob, FL_BLACK);
31         } else {
32                 fl_deactivate_object(ob);
33                 fl_set_object_lcol(ob, FL_INACTIVE);
34         }
35 }
36
37         
38 // Take a string and add breaks so that it fits into a desired label width, w
39 string formatted(string const & sin, int w, int size, int style)
40 {
41 #warning Why cant this be done by a one pass algo? (Lgb)
42         string sout;
43         if (sin.empty()) return sout;
44
45         // break sin up into a vector of individual words
46         vector<string> sentence;
47         string word;
48         for (string::const_iterator sit = sin.begin();
49              sit != sin.end(); ++sit) {
50                 if ((*sit) == ' ' || (*sit) == '\n') {
51                         sentence.push_back(word);
52                         word.erase();
53                         if ((*sit) == '\n') word += '\n';
54                         
55                 } else {
56                         word += (*sit);
57                 }
58         }
59
60         // Flush remaining contents of word
61         if (!word.empty() ) sentence.push_back(word);
62
63         string line;
64         string line_plus_word;
65         for (vector<string>::const_iterator vit = sentence.begin();
66              vit != sentence.end(); ++vit) {
67                 string word(*vit);
68
69                 char c = word[0];
70                 if (c == '\n') {
71                         sout += line + '\n';
72                         word = word.substr(1);
73                         line_plus_word.erase();
74                         line.erase();
75                 }
76
77                 if (!line_plus_word.empty() ) line_plus_word += ' ';
78                 line_plus_word += word;
79
80                 int const length = fl_get_string_width(style, size,
81                                                        line_plus_word.c_str(),
82                                                        int(line_plus_word.length()));
83                 if (length >= w) {
84                         sout += line + '\n';
85                         line_plus_word = word;
86                 }
87
88                 line = line_plus_word;
89         }
90         // Flush remaining contents of line
91         if (!line.empty()) {
92                 sout += line;
93         }
94
95         if (sout[sout.length() - 1] == '\n')
96                 sout.erase(sout.length() - 1);
97
98         return sout;
99 }
100
101
102 namespace {
103
104 // sorted by hand to prevent LyXLex from complaining on read().
105 keyword_item xformTags[] = {
106         { "\\gui_background", FL_COL1 },
107         { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
108         { "\\gui_buttonleft", FL_LEFT_BCOL },
109         { "\\gui_buttonright", FL_RIGHT_BCOL },
110         { "\\gui_buttontop", FL_TOP_BCOL },
111         { "\\gui_inactive", FL_INACTIVE },
112         { "\\gui_push_button", FL_YELLOW },
113         { "\\gui_selected", FL_MCOL },  
114         { "\\gui_text", FL_BLACK }
115 };
116
117
118 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
119
120 } // namespace anon
121
122
123 bool XformsColor::read(string const & filename)
124 {
125         LyXLex lexrc(xformTags, xformCount);
126         if (!lexrc.setFile(filename))
127                 return false;
128
129         while (lexrc.IsOK()) {
130                 int const le = lexrc.lex();
131
132                 switch (le) {
133                 case LyXLex::LEX_UNDEF:
134                         lexrc.printError("Unknown tag `$$Token'");
135                         continue; 
136                 case LyXLex::LEX_FEOF:
137                         continue;
138                 default: break;
139                 }
140
141                 RGBColor col;
142
143                 if (!lexrc.next()) break;
144                 col.r = lexrc.GetInteger();
145
146                 if (!lexrc.next()) break;
147                 col.g = lexrc.GetInteger();
148
149                 if (!lexrc.next()) break;
150                 col.b = lexrc.GetInteger();
151
152                 fl_mapcolor(le, col.r, col.g, col.b);
153         }
154                 
155         return true;
156 }
157
158
159 bool XformsColor::write(string const & filename)
160 {
161         ofstream os(filename.c_str());
162         if (!os)
163                 return false;
164
165         os << "### This file is part of\n"
166            << "### ========================================================\n"
167            << "###          LyX, The Document Processor\n"
168            << "###\n"
169            << "###          Copyright 1995 Matthias Ettrich\n"
170            << "###          Copyright 1995-2000 The LyX Team.\n"
171            << "###\n"
172            << "### ========================================================\n"
173            << "\n"
174            << "# This file is written by LyX, if you want to make your own\n"
175            << "# modifications you should do them from inside LyX and save\n"
176            << "\n";
177
178         for (int i = 0; i < xformCount; ++i) {
179                 string const tag  = xformTags[i].tag;
180                 int const colorID = xformTags[i].code;
181                 RGBColor color;
182
183                 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
184
185                 os << tag << " "
186                    << color.r << " " << color.g << " " << color.b << "\n";
187         }
188
189         return true;
190 }
191
192
193 string  RWInfo::error_message;
194
195 bool RWInfo::WriteableDir(string const & name)
196 {
197         error_message.erase();
198
199         if (!AbsolutePath(name)) {
200                 error_message = N_("The absolute path is required.");
201                 return false;
202         }
203
204         FileInfo const tp(name);
205         if (!tp.isDir()) {
206                 error_message = N_("Directory does not exist.");
207                 return false;
208         }
209
210         if (!tp.writable()) {
211                 error_message = N_("Cannot write to this directory.");
212                 return false;
213         }
214
215         return true;
216 }
217
218
219 bool RWInfo::ReadableDir(string const & name)
220 {
221         error_message.erase();
222
223         if (!AbsolutePath(name)) {
224                 error_message = N_("The absolute path is required.");
225                 return false;
226         }
227
228         FileInfo const tp(name);
229         if (!tp.isDir()) {
230                 error_message = N_("Directory does not exist.");
231                 return false;
232         }
233
234         if (!tp.readable()) {
235                 error_message = N_("Cannot read this directory.");
236                 return false;
237         }
238
239         return true;
240 }
241
242
243 bool RWInfo::WriteableFile(string const & name)
244 {
245         // A writeable file is either:
246         // * An existing file to which we have write access, or
247         // * A file that doesn't yet exist but that would exist in a writeable
248         //   directory.
249
250         error_message.erase();
251
252         if (name.empty()) {
253                 error_message = N_("No file input.");
254                 return false;
255         }
256
257         string const dir = OnlyPath(name);
258         if (!AbsolutePath(dir)) {
259                 error_message = N_("The absolute path is required.");
260                 return false;
261         }
262
263         FileInfo d(name);
264         if (!d.isDir()) {
265                 d.newFile(dir);
266         }
267
268         if (!d.isDir()) {
269                 error_message = N_("Directory does not exist.");
270                 return false;
271         }
272         
273         if (!d.writable()) {
274                 error_message = N_("Cannot write to this directory.");
275                 return false;
276         }
277
278         FileInfo f(name);
279         if (dir == name || f.isDir()) {
280                 error_message = N_("A file is required, not a directory.");
281                 return false;
282         }
283
284         if (f.exist() && !f.writable()) {
285                 error_message = N_("Cannot write to this file.");
286                 return false;
287         }
288         
289         return true;
290 }
291
292
293 bool RWInfo::ReadableFile(string const & name)
294 {
295         error_message.erase();
296
297         if (name.empty()) {
298                 error_message = N_("No file input.");
299                 return false;
300         }
301
302         string const dir = OnlyPath(name);
303         if (!AbsolutePath(dir)) {
304                 error_message = N_("The absolute path is required.");
305                 return false;
306         }
307
308         FileInfo d(name);
309         if (!d.isDir()) {
310                 d.newFile(dir);
311         }
312
313         if (!d.isDir()) {
314                 error_message = N_("Directory does not exist.");
315                 return false;
316         }
317         
318         if (!d.readable()) {
319                 error_message = N_("Cannot read from this directory.");
320                 return false;
321         }
322
323         FileInfo f(name);
324         if (dir == name || f.isDir()) {
325                 error_message = N_("A file is required, not a directory.");
326                 return false;
327         }
328
329         if (!f.exist()) {
330                 error_message = N_("File does not exist.");
331                 return false;
332         }
333         
334         if (!f.readable()) {
335                 error_message = N_("Cannot read from this file.");
336                 return false;
337         }
338
339         return true;
340 }