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