1 /** Collection of some useful xform helper functions
6 #include FORMS_H_LOCATION
8 #include <fstream> // ofstream
12 #pragma implementation
15 #include "xforms_helpers.h"
17 #include "support/FileInfo.h"
18 #include "support/filetools.h"
19 #include "support/lstrings.h" // frontStrip, strip
21 #include "support/LAssert.h"
22 #include "lyxlength.h"
23 #include "lyxgluelength.h"
29 // Extract shortcut from <ident>|<shortcut> string
30 char const * flyx_shortcut_extract(char const * sc)
32 // Find '|' in the sc and return the string after that.
33 register char const * sd = sc;
34 while (sd[0]!= 0 && sd[0] != '|') ++sd;
38 //lyxerr << sd << endl;
45 // Extract identifier from <ident>|<shortcut> string
46 char const * flyx_ident_extract(char const * sc)
48 register char const * se = sc;
49 while (se[0]!= 0 && se[0] != '|') ++se;
51 if (se[0] == 0) return sc;
53 char * sb = new char[se - sc + 1];
55 register char const * sd = sc;
65 // Set an FL_OBJECT to activated or deactivated
66 void setEnabled(FL_OBJECT * ob, bool enable)
69 fl_activate_object(ob);
70 fl_set_object_lcol(ob, FL_BLACK);
72 fl_deactivate_object(ob);
73 fl_set_object_lcol(ob, FL_INACTIVE);
78 // Given an fl_choice, create a vector of its entries
79 vector<string> const getVectorFromChoice(FL_OBJECT * ob)
82 if (!ob || ob->objclass != FL_CHOICE)
85 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
86 string const text = fl_get_choice_item_text(ob, i+1);
87 vec.push_back(strip(frontStrip(text)));
94 /// Given an fl_input, return its contents.
95 string const getStringFromInput(FL_OBJECT * ob)
97 if (!ob || ob->objclass != FL_INPUT)
100 char const * tmp = fl_get_input(ob);
101 return (tmp) ? tmp : string();
105 // Given an fl_browser, return the contents of line
106 string const getStringFromBrowser(FL_OBJECT * ob, int line)
108 if (!ob || ob->objclass != FL_BROWSER ||
109 line < 1 || line > fl_get_browser_maxline(ob))
112 char const * tmp = fl_get_browser_line(ob, line);
113 return (tmp) ? tmp : string();
116 // Given an fl_browser, return the contents of the currently
118 // If nothing is selected, return an empty string
119 string const getSelectedStringFromBrowser(FL_OBJECT * ob)
121 if (!ob || ob->objclass != FL_BROWSER)
124 int const line = fl_get_browser(ob);
125 if (line < 1 || line > fl_get_browser_maxline(ob))
128 if (!fl_isselected_browser_line(ob, line))
131 char const * tmp = fl_get_browser_line(ob, line);
132 return (tmp) ? tmp : string();
136 // Given an fl_browser, create a vector of its entries
137 vector<string> const getVectorFromBrowser(FL_OBJECT * ob)
140 if (!ob || ob->objclass != FL_BROWSER)
143 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
144 string const text = fl_get_browser_line(ob, i+1);
145 vec.push_back(strip(frontStrip(text)));
152 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
155 lyx::Assert(input && input->objclass == FL_INPUT &&
156 choice && choice->objclass == FL_CHOICE);
158 string const length = strip(frontStrip(fl_get_input(input)));
162 //don't return unit-from-choice if the input(field) contains a unit
163 if (isValidGlueLength(length))
166 string unit = strip(frontStrip(fl_get_choice_text(choice)));
167 unit = subst(unit, "%%", "%");
169 return length + unit;
174 // this should definitely be the other way around!!!
175 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
176 LyXLength const & len,
177 string const & default_unit)
180 updateWidgetsFromLengthString(input, choice,
181 string(), default_unit);
183 updateWidgetsFromLengthString(input, choice,
184 len.asString(), default_unit);
189 // Most of the code here is a poor duplication of the parser code
190 // which is in LyXLength. Use that instead
191 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
193 string const & default_unit)
196 lyx::Assert(input && input->objclass == FL_INPUT &&
197 choice && choice->objclass == FL_CHOICE);
200 fl_set_input(input, "");
201 int unitpos = 1; // xforms has Fortran-style indexing
202 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
203 string const text = fl_get_choice_item_text(choice,i+1);
205 lowercase(strip(frontStrip(text)))) {
210 fl_set_choice(choice, unitpos);
214 // The unit is presumed to begin at the first char a-z
215 // or with the char '%'
216 string const tmp = lowercase(strip(frontStrip(str)));
218 string::const_iterator p = tmp.begin();
219 for (; p != tmp.end(); ++p) {
220 if ((*p >= 'a' && *p <= 'z') || *p == '%')
225 int unitpos = 1; // xforms has Fortran-style indexing
227 if (p == tmp.end()) {
232 string tmplen = string(tmp.begin(), p);
233 if (isStrDbl(tmplen))
235 string unit = string(p, tmp.end());
236 unit = subst(unit, "%", "%%");
238 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
239 string const text = fl_get_choice_item_text(choice,i+1);
240 if (unit == lowercase(strip(frontStrip(text)))) {
247 fl_set_input(input, len.c_str());
248 fl_set_choice(choice, unitpos);
251 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
253 string const & default_unit)
255 updateWidgetsFromLength(input, choice,
256 LyXLength(str), default_unit);
260 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
261 LyXLength const & len,
262 string const & default_unit)
265 lyx::Assert(input && input->objclass == FL_INPUT &&
266 choice && choice->objclass == FL_CHOICE);
269 fl_set_input(input, "");
270 fl_set_choice_text(choice, default_unit.c_str());
272 ostringstream buffer;
273 buffer << len.value();
274 fl_set_input(input, buffer.str().c_str());
275 fl_set_choice_text(choice, stringFromUnit(len.unit()));
281 // Take a string and add breaks so that it fits into a desired label width, w
282 string formatted(string const & sin, int w, int size, int style)
284 // FIX: Q: Why cant this be done by a one pass algo? (Lgb)
287 if (sin.empty()) return sout;
289 // breaks in up into a vector of individual words
290 vector<string> sentence;
292 for (string::const_iterator sit = sin.begin();
293 sit != sin.end(); ++sit) {
294 if ((*sit) == ' ' || (*sit) == '\n') {
296 sentence.push_back(word);
299 if ((*sit) == '\n') word += '\n';
306 // Flush remaining contents of word
307 if (!word.empty() ) sentence.push_back(word);
310 string line_plus_word;
311 for (vector<string>::const_iterator vit = sentence.begin();
312 vit != sentence.end(); ++vit) {
319 line_plus_word.erase();
323 if (!line_plus_word.empty() ) line_plus_word += ' ';
324 line_plus_word += word;
326 int const length = fl_get_string_width(style, size,
327 line_plus_word.c_str(),
328 int(line_plus_word.length()));
331 line_plus_word = word;
334 line = line_plus_word;
336 // Flush remaining contents of line
341 if (sout[sout.length() - 1] == '\n')
342 sout.erase(sout.length() - 1);
350 // sorted by hand to prevent LyXLex from complaining on read().
351 keyword_item xformTags[] = {
352 { "\\gui_background", FL_COL1 },
353 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
354 { "\\gui_buttonleft", FL_LEFT_BCOL },
355 { "\\gui_buttonright", FL_RIGHT_BCOL },
356 { "\\gui_buttontop", FL_TOP_BCOL },
357 { "\\gui_inactive", FL_INACTIVE },
358 { "\\gui_push_button", FL_YELLOW },
359 { "\\gui_selected", FL_MCOL },
360 { "\\gui_text", FL_BLACK }
364 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
369 bool XformsColor::read(string const & filename)
371 LyXLex lexrc(xformTags, xformCount);
372 if (!lexrc.setFile(filename))
375 while (lexrc.isOK()) {
376 int const le = lexrc.lex();
379 case LyXLex::LEX_UNDEF:
380 lexrc.printError("Unknown tag `$$Token'");
382 case LyXLex::LEX_FEOF:
389 if (!lexrc.next()) break;
390 col.r = lexrc.getInteger();
392 if (!lexrc.next()) break;
393 col.g = lexrc.getInteger();
395 if (!lexrc.next()) break;
396 col.b = lexrc.getInteger();
398 fl_mapcolor(le, col.r, col.g, col.b);
405 bool XformsColor::write(string const & filename)
407 ofstream os(filename.c_str());
411 os << "### This file is part of\n"
412 << "### ========================================================\n"
413 << "### LyX, The Document Processor\n"
415 << "### Copyright 1995 Matthias Ettrich\n"
416 << "### Copyright 1995-2001 The LyX Team.\n"
418 << "### ========================================================\n"
420 << "# This file is written by LyX, if you want to make your own\n"
421 << "# modifications you should do them from inside LyX and save\n"
424 for (int i = 0; i < xformCount; ++i) {
425 string const tag = xformTags[i].tag;
426 int const colorID = xformTags[i].code;
429 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
432 << color.r << " " << color.g << " " << color.b << "\n";
439 string RWInfo::error_message;
441 bool RWInfo::WriteableDir(string const & name)
443 error_message.erase();
445 if (!AbsolutePath(name)) {
446 error_message = N_("The absolute path is required.");
450 FileInfo const tp(name);
451 if (!tp.isOK() || !tp.isDir()) {
452 error_message = N_("Directory does not exist.");
456 if (!tp.writable()) {
457 error_message = N_("Cannot write to this directory.");
465 bool RWInfo::ReadableDir(string const & name)
467 error_message.erase();
469 if (!AbsolutePath(name)) {
470 error_message = N_("The absolute path is required.");
474 FileInfo const tp(name);
475 if (!tp.isOK() || !tp.isDir()) {
476 error_message = N_("Directory does not exist.");
480 if (!tp.readable()) {
481 error_message = N_("Cannot read this directory.");
489 bool RWInfo::WriteableFile(string const & name)
491 // A writeable file is either:
492 // * An existing file to which we have write access, or
493 // * A file that doesn't yet exist but that would exist in a writeable
496 error_message.erase();
499 error_message = N_("No file input.");
503 string const dir = OnlyPath(name);
504 if (!AbsolutePath(dir)) {
505 error_message = N_("The absolute path is required.");
511 if (!d.isOK() || !d.isDir()) {
515 if (!d.isOK() || !d.isDir()) {
516 error_message = N_("Directory does not exist.");
521 error_message = N_("Cannot write to this directory.");
526 if (dir == name || (f.isOK() && f.isDir())) {
527 error_message = N_("A file is required, not a directory.");
531 if (f.isOK() && f.exist() && !f.writable()) {
532 error_message = N_("Cannot write to this file.");
540 bool RWInfo::ReadableFile(string const & name)
542 error_message.erase();
545 error_message = N_("No file input.");
549 string const dir = OnlyPath(name);
550 if (!AbsolutePath(dir)) {
551 error_message = N_("The absolute path is required.");
557 if (!d.isOK() && !d.isDir()) {
561 if (!d.isOK() || !d.isDir()) {
562 error_message = N_("Directory does not exist.");
567 error_message = N_("Cannot read from this directory.");
572 if (dir == name || (f.isOK() && f.isDir())) {
573 error_message = N_("A file is required, not a directory.");
578 error_message = N_("File does not exist.");
583 error_message = N_("Cannot read from this file.");