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