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