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