]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/xforms_helpers.C
xforms alert fixes
[lyx.git] / src / frontends / xforms / xforms_helpers.C
1 /**
2  * \file xforms_helpers.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13
14 #include "xforms_helpers.h"
15
16 #include "lyxlex.h"
17 #include "gettext.h"
18 #include "lyxlength.h"
19 #include "lyxgluelength.h"
20
21 #include "support/LAssert.h"
22 #include "support/FileInfo.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h" // frontStrip, strip
25
26 #include <fstream>
27
28 #include FORMS_H_LOCATION
29
30 using std::ofstream;
31 using std::pair;
32 using std::vector;
33 using std::make_pair;
34
35 bool isActive(FL_OBJECT * ob)
36 {
37         return ob && ob->active > 0;
38 }
39
40 std::pair<string, string> parse_shortcut(string const & str)
41 {
42         string::size_type i = str.find_first_of("&");
43         if (i == string::npos || i == str.length() - 1)
44                 return make_pair(str, string());
45
46         // FIXME: handle &&
47
48         string::value_type c = str[i + 1];
49         return make_pair(str.substr(0, i) + str.substr(i + 1),
50                 string("#") + c);
51 }
52
53
54 // A wrapper for the xforms routine, but this one accepts uint args
55 unsigned long fl_getmcolor(int i,
56                            unsigned int * r, unsigned int * g, unsigned int * b)
57 {
58         int r2, g2, b2;
59         unsigned long ret_val = ::fl_getmcolor(i, &r2, &g2, &b2);
60         *r = r2;
61         *g = g2;
62         *b = b2;
63         return ret_val;
64 }
65
66
67 // Set an FL_OBJECT to activated or deactivated
68 void setEnabled(FL_OBJECT * ob, bool enable)
69 {
70         if (enable) {
71                 fl_activate_object(ob);
72                 fl_set_object_lcol(ob, FL_LCOL);
73         } else {
74                 fl_deactivate_object(ob);
75                 fl_set_object_lcol(ob, FL_INACTIVE);
76         }
77 }
78
79
80 // Given an fl_choice or an fl_browser, create a vector of its entries
81 vector<string> const getVector(FL_OBJECT * ob)
82 {
83         vector <string> vec;
84
85         switch (ob->objclass) {
86         case FL_CHOICE:
87                 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
88                         string const text = fl_get_choice_item_text(ob, i+1);
89                         vec.push_back(trim(text));
90                 }
91                 break;
92         case FL_BROWSER:
93                 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
94                         string const text = fl_get_browser_line(ob, i+1);
95                         vec.push_back(trim(text));
96                 }
97                 break;
98         default:
99                 lyx::Assert(0);
100         }
101
102         return vec;
103 }
104
105
106 ///
107 string const getString(FL_OBJECT * ob, int line)
108 {
109         // Negative line value does not make sense.
110         lyx::Assert(line >= 0);
111
112         char const * tmp = 0;
113         switch (ob->objclass) {
114         case FL_INPUT:
115                 tmp = fl_get_input(ob);
116                 break;
117
118         case FL_BROWSER:
119                 if (line == 0)
120                         line = fl_get_browser(ob);
121
122                 if (line >= 1 && line <= fl_get_browser_maxline(ob))
123                         tmp = fl_get_browser_line(ob, line);
124                 break;
125
126         case FL_CHOICE:
127                 if (line == 0)
128                         line = fl_get_choice(ob);
129
130                 if (line >= 1 && line <= fl_get_choice_maxitems(ob))
131                         tmp = fl_get_choice_item_text(ob, line);
132                 break;
133
134         default:
135                 lyx::Assert(0);
136         }
137
138         return tmp ? trim(tmp) : string();
139 }
140
141 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
142 {
143         // Paranoia check
144         lyx::Assert(input  && input->objclass  == FL_INPUT &&
145                     choice && choice->objclass == FL_CHOICE);
146
147         string const length = trim(fl_get_input(input));
148         if (length.empty())
149                 return string();
150
151         // don't return unit-from-choice if the input(field) contains a unit
152         if (isValidGlueLength(length))
153                 return length;
154
155         string unit = trim(fl_get_choice_text(choice));
156         unit = subst(unit, "%%", "%");
157
158         return length + unit;
159 }
160
161
162 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
163                                    string const & str,
164                                    string const & default_unit)
165 {
166         // use input field only for gluelengths
167         if (!isValidLength(str) && !isStrDbl(str)) {
168                 fl_set_input(input, str.c_str());
169                 // we assume that "default_unit" is in the choice as "we"
170                 // have control over that!
171                 // No need to check for its presence in the choice, therefore.
172                 fl_set_choice_text(choice, default_unit.c_str());
173         } else {
174                 updateWidgetsFromLength(input, choice,
175                                 LyXLength(str), default_unit);
176         }
177 }
178
179
180 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
181                              LyXLength const & len,
182                              string const & default_unit)
183 {
184         // Paranoia check
185         lyx::Assert(input  && input->objclass  == FL_INPUT &&
186                     choice && choice->objclass == FL_CHOICE);
187
188         if (len.empty()) {
189                 fl_set_input(input, "");
190                 fl_set_choice_text(choice, default_unit.c_str());
191         } else {
192                 ostringstream buffer;
193                 buffer << len.value();
194                 fl_set_input(input, buffer.str().c_str());
195
196                 // Set the choice to the desired unit, if present in the choice.
197                 // Else set the choice to the default unit.
198                 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
199
200                 vector<string> const vec = getVector(choice);
201                 vector<string>::const_iterator it =
202                         std::find(vec.begin(), vec.end(), unit);
203                 if (it != vec.end()) {
204                         fl_set_choice_text(choice, unit.c_str());
205                 } else {
206                         fl_set_choice_text(choice, default_unit.c_str());
207                 }
208         }
209 }
210
211
212 // Take a string and add breaks so that it fits into a desired label width, w
213 string formatted(string const & sin, int w, int size, int style)
214 {
215         string sout;
216         if (sin.empty()) return sout;
217
218         string::size_type curpos = 0;
219         string line;
220         for(;;) {
221                 string::size_type const nxtpos1 = sin.find(' ',  curpos);
222                 string::size_type const nxtpos2 = sin.find('\n', curpos);
223                 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
224
225                 string const word = nxtpos == string::npos ?
226                         sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
227
228                 bool const newline = (nxtpos2 != string::npos &&
229                                       nxtpos2 < nxtpos1);
230
231                 string const line_plus_word =
232                         line.empty() ? word : line + ' ' + word;
233
234                 int const length =
235                         fl_get_string_width(style, size,
236                                             line_plus_word.c_str(),
237                                             int(line_plus_word.length()));
238
239                 if (length >= w) {
240                         sout += line + '\n';
241                         if (newline) {
242                                 sout += word + '\n';
243                                 line.erase();
244                         } else {
245                                 line = word;
246                         }
247
248                 } else if (newline) {
249                         sout += line_plus_word + '\n';
250                         line.erase();
251
252                 } else {
253                         if (!line.empty())
254                                 line += ' ';
255                         line += word;
256                 }
257
258                 if (nxtpos == string::npos) {
259                         if (!line.empty())
260                                 sout += line;
261                         break;
262                 }
263
264                 curpos = nxtpos+1;
265         }
266
267         return sout;
268 }
269
270
271 void setCursorColor(int color)
272 {
273         fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
274         fl_set_cursor_color(XC_xterm,          color, FL_WHITE);
275         fl_set_cursor_color(XC_watch,          color, FL_WHITE);
276         fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
277 }
278
279
280 namespace {
281
282 // sorted by hand to prevent LyXLex from complaining on read().
283 keyword_item xformTags[] = {
284         { "\\gui_background",   FL_COL1 },
285         { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
286         { "\\gui_buttonleft",   FL_LEFT_BCOL },
287         { "\\gui_buttonright",  FL_RIGHT_BCOL },
288         { "\\gui_buttontop",    FL_TOP_BCOL },
289         { "\\gui_inactive",     FL_INACTIVE },
290         { "\\gui_pointer",      FL_FREE_COL16 },
291         { "\\gui_push_button",  FL_YELLOW },
292         { "\\gui_selected",     FL_MCOL },
293         { "\\gui_text",         FL_BLACK }
294 };
295
296
297 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
298
299 } // namespace anon
300
301
302 bool XformsColor::read(string const & filename)
303 {
304         LyXLex lexrc(xformTags, xformCount);
305         if (!lexrc.setFile(filename))
306                 return false;
307
308         while (lexrc.isOK()) {
309                 int const le = lexrc.lex();
310
311                 switch (le) {
312                 case LyXLex::LEX_UNDEF:
313                         lexrc.printError("Unknown tag `$$Token'");
314                         continue;
315                 case LyXLex::LEX_FEOF:
316                         continue;
317                 default: break;
318                 }
319
320                 string const tag = lexrc.getString();
321
322                 RGBColor col;
323
324                 if (!lexrc.next()) break;
325                 col.r = lexrc.getInteger();
326
327                 if (!lexrc.next()) break;
328                 col.g = lexrc.getInteger();
329
330                 if (!lexrc.next()) break;
331                 col.b = lexrc.getInteger();
332
333                 fl_mapcolor(le, col.r, col.g, col.b);
334
335                 if (tag == "\\gui_pointer") {
336                         setCursorColor(FL_FREE_COL16);
337                 }
338         }
339
340         return true;
341 }
342
343
344 bool XformsColor::write(string const & filename)
345 {
346         ofstream os(filename.c_str());
347         if (!os)
348                 return false;
349
350         os << "###"
351            << "### file " << filename << "\n\n"
352            << "### This file is written by LyX, if you want to make your own\n"
353            << "### modifications you should do them from inside LyX and save\n"
354            << '\n';
355
356         for (int i = 0; i < xformCount; ++i) {
357                 string const tag  = xformTags[i].tag;
358                 int const colorID = xformTags[i].code;
359                 RGBColor color;
360
361                 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
362
363                 os << tag << ' '
364                    << color.r << ' ' << color.g << ' ' << color.b << '\n';
365         }
366
367         return true;
368 }
369
370
371 string  RWInfo::error_message;
372
373 bool RWInfo::WriteableDir(string const & name)
374 {
375         error_message.erase();
376
377         if (!AbsolutePath(name)) {
378                 error_message = _("The absolute path is required.");
379                 return false;
380         }
381
382         FileInfo const tp(name);
383         if (!tp.isOK() || !tp.isDir()) {
384                 error_message = _("Directory does not exist.");
385                 return false;
386         }
387
388         if (!tp.writable()) {
389                 error_message = _("Cannot write to this directory.");
390                 return false;
391         }
392
393         return true;
394 }
395
396
397 bool RWInfo::ReadableDir(string const & name)
398 {
399         error_message.erase();
400
401         if (!AbsolutePath(name)) {
402                 error_message = _("The absolute path is required.");
403                 return false;
404         }
405
406         FileInfo const tp(name);
407         if (!tp.isOK() || !tp.isDir()) {
408                 error_message = _("Directory does not exist.");
409                 return false;
410         }
411
412         if (!tp.readable()) {
413                 error_message = _("Cannot read this directory.");
414                 return false;
415         }
416
417         return true;
418 }
419
420
421 bool RWInfo::WriteableFile(string const & name)
422 {
423         // A writeable file is either:
424         // * An existing file to which we have write access, or
425         // * A file that doesn't yet exist but that would exist in a writeable
426         //   directory.
427
428         error_message.erase();
429
430         if (name.empty()) {
431                 error_message = _("No file input.");
432                 return false;
433         }
434
435         string const dir = OnlyPath(name);
436         if (!AbsolutePath(dir)) {
437                 error_message = _("The absolute path is required.");
438                 return false;
439         }
440
441         FileInfo d(name);
442
443         if (!d.isOK() || !d.isDir()) {
444                 d.newFile(dir);
445         }
446
447         if (!d.isOK() || !d.isDir()) {
448                 error_message = _("Directory does not exist.");
449                 return false;
450         }
451
452         if (!d.writable()) {
453                 error_message = _("Cannot write to this directory.");
454                 return false;
455         }
456
457         FileInfo f(name);
458         if (dir == name || (f.isOK() && f.isDir())) {
459                 error_message = _("A file is required, not a directory.");
460                 return false;
461         }
462
463         if (f.isOK() && f.exist() && !f.writable()) {
464                 error_message = _("Cannot write to this file.");
465                 return false;
466         }
467
468         return true;
469 }
470
471
472 bool RWInfo::ReadableFile(string const & name)
473 {
474         error_message.erase();
475
476         if (name.empty()) {
477                 error_message = _("No file input.");
478                 return false;
479         }
480
481         string const dir = OnlyPath(name);
482         if (!AbsolutePath(dir)) {
483                 error_message = _("The absolute path is required.");
484                 return false;
485         }
486
487         FileInfo d(name);
488
489         if (!d.isOK() && !d.isDir()) {
490                 d.newFile(dir);
491         }
492
493         if (!d.isOK() || !d.isDir()) {
494                 error_message = _("Directory does not exist.");
495                 return false;
496         }
497
498         if (!d.readable()) {
499                 error_message = _("Cannot read from this directory.");
500                 return false;
501         }
502
503         FileInfo f(name);
504         if (dir == name || (f.isOK() && f.isDir())) {
505                 error_message = _("A file is required, not a directory.");
506                 return false;
507         }
508
509         if (!f.exist()) {
510                 error_message = _("File does not exist.");
511                 return false;
512         }
513
514         if (!f.readable()) {
515                 error_message = _("Cannot read from this file.");
516                 return false;
517         }
518
519         return true;
520 }