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 string const tmp = lowercase(strip(frontStrip(str)));
217 string::const_iterator p = tmp.begin();
218 for (; p != tmp.end(); ++p) {
219 if (*p >= 'a' && *p <= 'z')
224 int unitpos = 1; // xforms has Fortran-style indexing
226 if (p == tmp.end()) {
231 string tmplen = string(tmp.begin(), p);
232 if (isStrDbl(tmplen))
234 string unit = string(p, tmp.end());
235 unit = subst(unit, "%", "%%");
237 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
238 string const text = fl_get_choice_item_text(choice,i+1);
239 if (unit == lowercase(strip(frontStrip(text)))) {
246 fl_set_input(input, len.c_str());
247 fl_set_choice(choice, unitpos);
250 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
252 string const & default_unit)
254 updateWidgetsFromLength(input, choice,
255 LyXLength(str), default_unit);
259 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
260 LyXLength const & len,
261 string const & default_unit)
264 lyx::Assert(input && input->objclass == FL_INPUT &&
265 choice && choice->objclass == FL_CHOICE);
268 fl_set_input(input, "");
269 fl_set_choice_text(choice, default_unit.c_str());
271 ostringstream buffer;
272 buffer << len.value();
273 fl_set_input(input, buffer.str().c_str());
274 fl_set_choice_text(choice, stringFromUnit(len.unit()));
280 // Take a string and add breaks so that it fits into a desired label width, w
281 string formatted(string const & sin, int w, int size, int style)
283 // FIX: Q: Why cant this be done by a one pass algo? (Lgb)
286 if (sin.empty()) return sout;
288 // breaks in up into a vector of individual words
289 vector<string> sentence;
291 for (string::const_iterator sit = sin.begin();
292 sit != sin.end(); ++sit) {
293 if ((*sit) == ' ' || (*sit) == '\n') {
295 sentence.push_back(word);
298 if ((*sit) == '\n') word += '\n';
305 // Flush remaining contents of word
306 if (!word.empty() ) sentence.push_back(word);
309 string line_plus_word;
310 for (vector<string>::const_iterator vit = sentence.begin();
311 vit != sentence.end(); ++vit) {
318 line_plus_word.erase();
322 if (!line_plus_word.empty() ) line_plus_word += ' ';
323 line_plus_word += word;
325 int const length = fl_get_string_width(style, size,
326 line_plus_word.c_str(),
327 int(line_plus_word.length()));
330 line_plus_word = word;
333 line = line_plus_word;
335 // Flush remaining contents of line
340 if (sout[sout.length() - 1] == '\n')
341 sout.erase(sout.length() - 1);
349 // sorted by hand to prevent LyXLex from complaining on read().
350 keyword_item xformTags[] = {
351 { "\\gui_background", FL_COL1 },
352 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
353 { "\\gui_buttonleft", FL_LEFT_BCOL },
354 { "\\gui_buttonright", FL_RIGHT_BCOL },
355 { "\\gui_buttontop", FL_TOP_BCOL },
356 { "\\gui_inactive", FL_INACTIVE },
357 { "\\gui_push_button", FL_YELLOW },
358 { "\\gui_selected", FL_MCOL },
359 { "\\gui_text", FL_BLACK }
363 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
368 bool XformsColor::read(string const & filename)
370 LyXLex lexrc(xformTags, xformCount);
371 if (!lexrc.setFile(filename))
374 while (lexrc.isOK()) {
375 int const le = lexrc.lex();
378 case LyXLex::LEX_UNDEF:
379 lexrc.printError("Unknown tag `$$Token'");
381 case LyXLex::LEX_FEOF:
388 if (!lexrc.next()) break;
389 col.r = lexrc.getInteger();
391 if (!lexrc.next()) break;
392 col.g = lexrc.getInteger();
394 if (!lexrc.next()) break;
395 col.b = lexrc.getInteger();
397 fl_mapcolor(le, col.r, col.g, col.b);
404 bool XformsColor::write(string const & filename)
406 ofstream os(filename.c_str());
410 os << "### This file is part of\n"
411 << "### ========================================================\n"
412 << "### LyX, The Document Processor\n"
414 << "### Copyright 1995 Matthias Ettrich\n"
415 << "### Copyright 1995-2001 The LyX Team.\n"
417 << "### ========================================================\n"
419 << "# This file is written by LyX, if you want to make your own\n"
420 << "# modifications you should do them from inside LyX and save\n"
423 for (int i = 0; i < xformCount; ++i) {
424 string const tag = xformTags[i].tag;
425 int const colorID = xformTags[i].code;
428 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
431 << color.r << " " << color.g << " " << color.b << "\n";
438 string RWInfo::error_message;
440 bool RWInfo::WriteableDir(string const & name)
442 error_message.erase();
444 if (!AbsolutePath(name)) {
445 error_message = N_("The absolute path is required.");
449 FileInfo const tp(name);
450 if (!tp.isOK() || !tp.isDir()) {
451 error_message = N_("Directory does not exist.");
455 if (!tp.writable()) {
456 error_message = N_("Cannot write to this directory.");
464 bool RWInfo::ReadableDir(string const & name)
466 error_message.erase();
468 if (!AbsolutePath(name)) {
469 error_message = N_("The absolute path is required.");
473 FileInfo const tp(name);
474 if (!tp.isOK() || !tp.isDir()) {
475 error_message = N_("Directory does not exist.");
479 if (!tp.readable()) {
480 error_message = N_("Cannot read this directory.");
488 bool RWInfo::WriteableFile(string const & name)
490 // A writeable file is either:
491 // * An existing file to which we have write access, or
492 // * A file that doesn't yet exist but that would exist in a writeable
495 error_message.erase();
498 error_message = N_("No file input.");
502 string const dir = OnlyPath(name);
503 if (!AbsolutePath(dir)) {
504 error_message = N_("The absolute path is required.");
510 if (!d.isOK() || !d.isDir()) {
514 if (!d.isOK() || !d.isDir()) {
515 error_message = N_("Directory does not exist.");
520 error_message = N_("Cannot write to this directory.");
525 if (dir == name || (f.isOK() && f.isDir())) {
526 error_message = N_("A file is required, not a directory.");
530 if (f.isOK() && f.exist() && !f.writable()) {
531 error_message = N_("Cannot write to this file.");
539 bool RWInfo::ReadableFile(string const & name)
541 error_message.erase();
544 error_message = N_("No file input.");
548 string const dir = OnlyPath(name);
549 if (!AbsolutePath(dir)) {
550 error_message = N_("The absolute path is required.");
556 if (!d.isOK() && !d.isDir()) {
560 if (!d.isOK() || !d.isDir()) {
561 error_message = N_("Directory does not exist.");
566 error_message = N_("Cannot read from this directory.");
571 if (dir == name || (f.isOK() && f.isDir())) {
572 error_message = N_("A file is required, not a directory.");
577 error_message = N_("File does not exist.");
582 error_message = N_("Cannot read from this file.");