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