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