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