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