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"
28 // Extract shortcut from <ident>|<shortcut> string
29 char const * flyx_shortcut_extract(char const * sc)
31 // Find '|' in the sc and return the string after that.
32 register char const * sd = sc;
33 while (sd[0]!= 0 && sd[0] != '|') ++sd;
37 //lyxerr << sd << endl;
44 // Extract identifier from <ident>|<shortcut> string
45 char const * flyx_ident_extract(char const * sc)
47 register char const * se = sc;
48 while (se[0]!= 0 && se[0] != '|') ++se;
50 if (se[0] == 0) return sc;
52 char * sb = new char[se - sc + 1];
54 register char const * sd = sc;
64 // Set an FL_OBJECT to activated or deactivated
65 void setEnabled(FL_OBJECT * ob, bool enable)
68 fl_activate_object(ob);
69 fl_set_object_lcol(ob, FL_BLACK);
71 fl_deactivate_object(ob);
72 fl_set_object_lcol(ob, FL_INACTIVE);
77 // Given an fl_choice, create a vector of its entries
78 vector<string> const getVectorFromChoice(FL_OBJECT * ob)
81 if (!ob || ob->objclass != FL_CHOICE)
84 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
85 string const text = fl_get_choice_item_text(ob, i+1);
86 vec.push_back(strip(frontStrip(text)));
93 // Given an fl_browser, return the contents of line
94 string const getStringFromBrowser(FL_OBJECT * ob, int line)
96 if (!ob || ob->objclass != FL_BROWSER ||
97 line < 1 || line > fl_get_browser_maxline(ob))
100 char const * tmp = fl_get_browser_line(ob, line);
101 return (tmp) ? tmp : string();
104 // Given an fl_browser, return the contents of the currently
106 // If nothing is selected, return an empty string
107 string const getSelectedStringFromBrowser(FL_OBJECT * ob)
109 if (!ob || ob->objclass != FL_BROWSER)
112 int const line = fl_get_browser(ob);
113 if (line < 1 || line > fl_get_browser_maxline(ob))
116 if (!fl_isselected_browser_line(ob, line))
119 char const * tmp = fl_get_browser_line(ob, line);
120 return (tmp) ? tmp : string();
124 // Given an fl_browser, create a vector of its entries
125 vector<string> const getVectorFromBrowser(FL_OBJECT * ob)
128 if (!ob || ob->objclass != FL_BROWSER)
131 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
132 string const text = fl_get_browser_line(ob, i+1);
133 vec.push_back(strip(frontStrip(text)));
140 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
143 lyx::Assert(input && input->objclass == FL_INPUT &&
144 choice && choice->objclass == FL_CHOICE);
146 string const length = strip(frontStrip(fl_get_input(input)));
150 string unit = strip(frontStrip(fl_get_choice_text(choice)));
151 unit = subst(unit, "%%", "%");
153 return length + unit;
158 // this should definitely be the other way around!!!
159 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
160 LyXLength const & len,
161 string const & default_unit)
164 updateWidgetsFromLengthString(input, choice,
165 string(), default_unit);
167 updateWidgetsFromLengthString(input, choice,
168 len.asString(), default_unit);
173 // Most of the code here is a poor duplication of the parser code
174 // which is in LyXLength. Use that instead
175 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
177 string const & default_unit)
180 lyx::Assert(input && input->objclass == FL_INPUT &&
181 choice && choice->objclass == FL_CHOICE);
184 fl_set_input(input, "");
185 int unitpos = 1; // xforms has Fortran-style indexing
186 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
187 string const text = fl_get_choice_item_text(choice,i+1);
189 lowercase(strip(frontStrip(text)))) {
194 fl_set_choice(choice, unitpos);
198 // The unit is presumed to begin at the first char a-z
199 string const tmp = lowercase(strip(frontStrip(str)));
201 string::const_iterator p = tmp.begin();
202 for (; p != tmp.end(); ++p) {
203 if (*p >= 'a' && *p <= 'z')
208 int unitpos = 1; // xforms has Fortran-style indexing
210 if (p == tmp.end()) {
215 string tmplen = string(tmp.begin(), p);
216 if (isStrDbl(tmplen))
218 string unit = string(p, tmp.end());
219 unit = subst(unit, "%", "%%");
221 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
222 string const text = fl_get_choice_item_text(choice,i+1);
223 if (unit == lowercase(strip(frontStrip(text)))) {
230 fl_set_input(input, len.c_str());
231 fl_set_choice(choice, unitpos);
234 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
236 string const & default_unit)
238 updateWidgetsFromLength(input, choice,
239 LyXLength(str), default_unit);
243 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
244 LyXLength const & len,
245 string const & default_unit)
248 lyx::Assert(input && input->objclass == FL_INPUT &&
249 choice && choice->objclass == FL_CHOICE);
252 fl_set_input(input, "");
253 fl_set_choice_text(choice, default_unit.c_str());
255 ostringstream buffer;
256 buffer << len.value();
257 fl_set_input(input, buffer.str().c_str());
258 fl_set_choice_text(choice, stringFromUnit(len.unit()));
264 // Take a string and add breaks so that it fits into a desired label width, w
265 string formatted(string const & sin, int w, int size, int style)
267 // FIX: Q: Why cant this be done by a one pass algo? (Lgb)
270 if (sin.empty()) return sout;
272 // breaks in up into a vector of individual words
273 vector<string> sentence;
275 for (string::const_iterator sit = sin.begin();
276 sit != sin.end(); ++sit) {
277 if ((*sit) == ' ' || (*sit) == '\n') {
279 sentence.push_back(word);
282 if ((*sit) == '\n') word += '\n';
289 // Flush remaining contents of word
290 if (!word.empty() ) sentence.push_back(word);
293 string line_plus_word;
294 for (vector<string>::const_iterator vit = sentence.begin();
295 vit != sentence.end(); ++vit) {
302 line_plus_word.erase();
306 if (!line_plus_word.empty() ) line_plus_word += ' ';
307 line_plus_word += word;
309 int const length = fl_get_string_width(style, size,
310 line_plus_word.c_str(),
311 int(line_plus_word.length()));
314 line_plus_word = word;
317 line = line_plus_word;
319 // Flush remaining contents of line
324 if (sout[sout.length() - 1] == '\n')
325 sout.erase(sout.length() - 1);
333 // sorted by hand to prevent LyXLex from complaining on read().
334 keyword_item xformTags[] = {
335 { "\\gui_background", FL_COL1 },
336 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
337 { "\\gui_buttonleft", FL_LEFT_BCOL },
338 { "\\gui_buttonright", FL_RIGHT_BCOL },
339 { "\\gui_buttontop", FL_TOP_BCOL },
340 { "\\gui_inactive", FL_INACTIVE },
341 { "\\gui_push_button", FL_YELLOW },
342 { "\\gui_selected", FL_MCOL },
343 { "\\gui_text", FL_BLACK }
347 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
352 bool XformsColor::read(string const & filename)
354 LyXLex lexrc(xformTags, xformCount);
355 if (!lexrc.setFile(filename))
358 while (lexrc.isOK()) {
359 int const le = lexrc.lex();
362 case LyXLex::LEX_UNDEF:
363 lexrc.printError("Unknown tag `$$Token'");
365 case LyXLex::LEX_FEOF:
372 if (!lexrc.next()) break;
373 col.r = lexrc.getInteger();
375 if (!lexrc.next()) break;
376 col.g = lexrc.getInteger();
378 if (!lexrc.next()) break;
379 col.b = lexrc.getInteger();
381 fl_mapcolor(le, col.r, col.g, col.b);
388 bool XformsColor::write(string const & filename)
390 ofstream os(filename.c_str());
394 os << "### This file is part of\n"
395 << "### ========================================================\n"
396 << "### LyX, The Document Processor\n"
398 << "### Copyright 1995 Matthias Ettrich\n"
399 << "### Copyright 1995-2001 The LyX Team.\n"
401 << "### ========================================================\n"
403 << "# This file is written by LyX, if you want to make your own\n"
404 << "# modifications you should do them from inside LyX and save\n"
407 for (int i = 0; i < xformCount; ++i) {
408 string const tag = xformTags[i].tag;
409 int const colorID = xformTags[i].code;
412 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
415 << color.r << " " << color.g << " " << color.b << "\n";
422 string RWInfo::error_message;
424 bool RWInfo::WriteableDir(string const & name)
426 error_message.erase();
428 if (!AbsolutePath(name)) {
429 error_message = N_("The absolute path is required.");
433 FileInfo const tp(name);
434 if (!tp.isOK() || !tp.isDir()) {
435 error_message = N_("Directory does not exist.");
439 if (!tp.writable()) {
440 error_message = N_("Cannot write to this directory.");
448 bool RWInfo::ReadableDir(string const & name)
450 error_message.erase();
452 if (!AbsolutePath(name)) {
453 error_message = N_("The absolute path is required.");
457 FileInfo const tp(name);
458 if (!tp.isOK() || !tp.isDir()) {
459 error_message = N_("Directory does not exist.");
463 if (!tp.readable()) {
464 error_message = N_("Cannot read this directory.");
472 bool RWInfo::WriteableFile(string const & name)
474 // A writeable file is either:
475 // * An existing file to which we have write access, or
476 // * A file that doesn't yet exist but that would exist in a writeable
479 error_message.erase();
482 error_message = N_("No file input.");
486 string const dir = OnlyPath(name);
487 if (!AbsolutePath(dir)) {
488 error_message = N_("The absolute path is required.");
494 if (!d.isOK() || !d.isDir()) {
498 if (!d.isOK() || !d.isDir()) {
499 error_message = N_("Directory does not exist.");
504 error_message = N_("Cannot write to this directory.");
509 if (dir == name || (f.isOK() && f.isDir())) {
510 error_message = N_("A file is required, not a directory.");
514 if (f.isOK() && f.exist() && !f.writable()) {
515 error_message = N_("Cannot write to this file.");
523 bool RWInfo::ReadableFile(string const & name)
525 error_message.erase();
528 error_message = N_("No file input.");
532 string const dir = OnlyPath(name);
533 if (!AbsolutePath(dir)) {
534 error_message = N_("The absolute path is required.");
540 if (!d.isOK() && !d.isDir()) {
544 if (!d.isOK() || !d.isDir()) {
545 error_message = N_("Directory does not exist.");
550 error_message = N_("Cannot read from this directory.");
555 if (dir == name || (f.isOK() && f.isDir())) {
556 error_message = N_("A file is required, not a directory.");
561 error_message = N_("File does not exist.");
566 error_message = N_("Cannot read from this file.");