]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/xforms_helpers.C
Prepare the way for resizable tabfolders if the xforms version is
[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 #include "combox.h"
30
31 using std::ofstream;
32 using std::pair;
33 using std::vector;
34 using std::make_pair;
35
36
37 bool isActive(FL_OBJECT * ob)
38 {
39         return ob && ob->active > 0;
40 }
41
42
43 std::pair<string, string> parse_shortcut(string const & str)
44 {
45         string::size_type i = str.find_first_of("&");
46         if (i == string::npos || i == str.length() - 1)
47                 return make_pair(str, string());
48
49         // FIXME: handle &&
50
51         string::value_type c = str[i + 1];
52         return make_pair(str.substr(0, i) + str.substr(i + 1),
53                          string("#") + c);
54 }
55
56
57 // A wrapper for the xforms routine, but this one accepts uint args
58 unsigned long fl_getmcolor(int i,
59                            unsigned int * r, unsigned int * g, unsigned int * b)
60 {
61         int r2, g2, b2;
62         unsigned long ret_val = ::fl_getmcolor(i, &r2, &g2, &b2);
63         *r = r2;
64         *g = g2;
65         *b = b2;
66         return ret_val;
67 }
68
69
70 // Set an FL_OBJECT to activated or deactivated
71 void setEnabled(FL_OBJECT * ob, bool enable)
72 {
73         if (enable) {
74                 fl_activate_object(ob);
75                 fl_set_object_lcol(ob, FL_LCOL);
76         } else {
77                 fl_deactivate_object(ob);
78                 fl_set_object_lcol(ob, FL_INACTIVE);
79         }
80 }
81
82
83 // Given an fl_choice or an fl_browser, create a vector of its entries
84 vector<string> const getVector(FL_OBJECT * ob)
85 {
86         vector <string> vec;
87
88         switch (ob->objclass) {
89         case FL_CHOICE:
90                 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
91                         string const text = fl_get_choice_item_text(ob, i+1);
92                         vec.push_back(trim(text));
93                 }
94                 break;
95         case FL_BROWSER:
96                 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
97                         string const text = fl_get_browser_line(ob, i+1);
98                         vec.push_back(trim(text));
99                 }
100                 break;
101         default:
102                 lyx::Assert(0);
103         }
104
105         return vec;
106 }
107
108
109 ///
110 string const getString(FL_OBJECT * ob, int line)
111 {
112         // Negative line value does not make sense.
113         lyx::Assert(line >= 0);
114
115         char const * tmp = 0;
116         switch (ob->objclass) {
117         case FL_INPUT:
118                 tmp = fl_get_input(ob);
119                 break;
120
121         case FL_BROWSER:
122                 if (line == 0)
123                         line = fl_get_browser(ob);
124
125                 if (line >= 1 && line <= fl_get_browser_maxline(ob))
126                         tmp = fl_get_browser_line(ob, line);
127                 break;
128
129         case FL_CHOICE:
130                 if (line == 0)
131                         line = fl_get_choice(ob);
132
133                 if (line >= 1 && line <= fl_get_choice_maxitems(ob))
134                         tmp = fl_get_choice_item_text(ob, line);
135                 break;
136
137         case FL_COMBOX:
138                 tmp = fl_get_combox_text(ob);
139                 break;
140
141         default:
142                 lyx::Assert(0);
143         }
144
145         return tmp ? trim(tmp) : string();
146 }
147
148 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
149 {
150         // Paranoia check
151         lyx::Assert(input  && input->objclass  == FL_INPUT &&
152                     choice && choice->objclass == FL_CHOICE);
153
154         string const length = trim(fl_get_input(input));
155         if (length.empty())
156                 return string();
157
158         // don't return unit-from-choice if the input(field) contains a unit
159         if (isValidGlueLength(length))
160                 return length;
161
162         string unit = trim(fl_get_choice_text(choice));
163         unit = subst(unit, "%%", "%");
164
165         return length + unit;
166 }
167
168
169 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
170                                    string const & str,
171                                    string const & default_unit)
172 {
173         // use input field only for gluelengths
174         if (!isValidLength(str) && !isStrDbl(str)) {
175                 fl_set_input(input, str.c_str());
176                 // we assume that "default_unit" is in the choice as "we"
177                 // have control over that!
178                 // No need to check for its presence in the choice, therefore.
179                 fl_set_choice_text(choice, default_unit.c_str());
180         } else {
181                 updateWidgetsFromLength(input, choice,
182                                         LyXLength(str), default_unit);
183         }
184 }
185
186
187 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
188                              LyXLength const & len,
189                              string const & default_unit)
190 {
191         // Paranoia check
192         lyx::Assert(input  && input->objclass  == FL_INPUT &&
193                     choice && choice->objclass == FL_CHOICE);
194
195         if (len.empty()) {
196                 fl_set_input(input, "");
197                 fl_set_choice_text(choice, default_unit.c_str());
198         } else {
199                 ostringstream buffer;
200                 buffer << len.value();
201                 fl_set_input(input, buffer.str().c_str());
202
203                 // Set the choice to the desired unit, if present in the choice.
204                 // Else set the choice to the default unit.
205                 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
206
207                 vector<string> const vec = getVector(choice);
208                 vector<string>::const_iterator it =
209                         std::find(vec.begin(), vec.end(), unit);
210                 if (it != vec.end()) {
211                         fl_set_choice_text(choice, unit.c_str());
212                 } else {
213                         fl_set_choice_text(choice, default_unit.c_str());
214                 }
215         }
216 }
217
218
219 // Take a string and add breaks so that it fits into a desired label width, w
220 string formatted(string const & sin, int w, int size, int style)
221 {
222         string sout;
223         if (sin.empty()) 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 }