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