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 the currently
95 // If nothing is selected, return an empty string
96 string const getStringFromBrowser(FL_OBJECT * ob, int line)
98 if (!ob || ob->objclass != FL_BROWSER ||
99 line < 1 || line > fl_get_browser_maxline(ob))
102 char const * tmp = fl_get_browser_line(ob, line);
103 return (tmp) ? tmp : string();
106 // Given an fl_browser, create a vector of its entries
107 vector<string> const getVectorFromBrowser(FL_OBJECT * ob)
110 if (!ob || ob->objclass != FL_BROWSER)
113 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
114 string const text = fl_get_browser_line(ob, i+1);
115 vec.push_back(strip(frontStrip(text)));
122 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
125 lyx::Assert(input && input->objclass == FL_INPUT &&
126 choice && choice->objclass == FL_CHOICE);
128 string const length = strip(frontStrip(fl_get_input(input)));
132 string unit = strip(frontStrip(fl_get_choice_text(choice)));
133 unit = subst(unit, "%%", "%");
135 return length + unit;
140 // this should definitely be the other way around!!!
141 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
142 LyXLength const & len,
143 string const & default_unit)
146 updateWidgetsFromLengthString(input, choice,
147 string(), default_unit);
149 updateWidgetsFromLengthString(input, choice,
150 len.asString(), default_unit);
155 // Most of the code here is a poor duplication of the parser code
156 // which is in LyXLength. Use that instead
157 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
159 string const & default_unit)
162 lyx::Assert(input && input->objclass == FL_INPUT &&
163 choice && choice->objclass == FL_CHOICE);
166 fl_set_input(input, "");
167 int unitpos = 1; // xforms has Fortran-style indexing
168 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
169 string const text = fl_get_choice_item_text(choice,i+1);
171 lowercase(strip(frontStrip(text)))) {
176 fl_set_choice(choice, unitpos);
180 // The unit is presumed to begin at the first char a-z
181 string const tmp = lowercase(strip(frontStrip(str)));
183 string::const_iterator p = tmp.begin();
184 for (; p != tmp.end(); ++p) {
185 if (*p >= 'a' && *p <= 'z')
190 int unitpos = 1; // xforms has Fortran-style indexing
192 if (p == tmp.end()) {
197 string tmplen = string(tmp.begin(), p);
198 if (isStrDbl(tmplen))
200 string unit = string(p, tmp.end());
201 unit = subst(unit, "%", "%%");
203 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
204 string const text = fl_get_choice_item_text(choice,i+1);
205 if (unit == lowercase(strip(frontStrip(text)))) {
212 fl_set_input(input, len.c_str());
213 fl_set_choice(choice, unitpos);
216 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
218 string const & default_unit)
220 updateWidgetsFromLength(input, choice,
221 LyXLength(str), default_unit);
225 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
226 LyXLength const & len,
227 string const & default_unit)
230 lyx::Assert(input && input->objclass == FL_INPUT &&
231 choice && choice->objclass == FL_CHOICE);
234 fl_set_input(input, "");
235 fl_set_choice_text(choice, default_unit.c_str());
237 ostringstream buffer;
238 buffer << len.value();
239 fl_set_input(input, buffer.str().c_str());
240 fl_set_choice_text(choice, stringFromUnit(len.unit()));
246 // Take a string and add breaks so that it fits into a desired label width, w
247 string formatted(string const & sin, int w, int size, int style)
249 // FIX: Q: Why cant this be done by a one pass algo? (Lgb)
252 if (sin.empty()) return sout;
254 // breaks in up into a vector of individual words
255 vector<string> sentence;
257 for (string::const_iterator sit = sin.begin();
258 sit != sin.end(); ++sit) {
259 if ((*sit) == ' ' || (*sit) == '\n') {
261 sentence.push_back(word);
264 if ((*sit) == '\n') word += '\n';
271 // Flush remaining contents of word
272 if (!word.empty() ) sentence.push_back(word);
275 string line_plus_word;
276 for (vector<string>::const_iterator vit = sentence.begin();
277 vit != sentence.end(); ++vit) {
284 line_plus_word.erase();
288 if (!line_plus_word.empty() ) line_plus_word += ' ';
289 line_plus_word += word;
291 int const length = fl_get_string_width(style, size,
292 line_plus_word.c_str(),
293 int(line_plus_word.length()));
296 line_plus_word = word;
299 line = line_plus_word;
301 // Flush remaining contents of line
306 if (sout[sout.length() - 1] == '\n')
307 sout.erase(sout.length() - 1);
315 // sorted by hand to prevent LyXLex from complaining on read().
316 keyword_item xformTags[] = {
317 { "\\gui_background", FL_COL1 },
318 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
319 { "\\gui_buttonleft", FL_LEFT_BCOL },
320 { "\\gui_buttonright", FL_RIGHT_BCOL },
321 { "\\gui_buttontop", FL_TOP_BCOL },
322 { "\\gui_inactive", FL_INACTIVE },
323 { "\\gui_push_button", FL_YELLOW },
324 { "\\gui_selected", FL_MCOL },
325 { "\\gui_text", FL_BLACK }
329 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
334 bool XformsColor::read(string const & filename)
336 LyXLex lexrc(xformTags, xformCount);
337 if (!lexrc.setFile(filename))
340 while (lexrc.isOK()) {
341 int const le = lexrc.lex();
344 case LyXLex::LEX_UNDEF:
345 lexrc.printError("Unknown tag `$$Token'");
347 case LyXLex::LEX_FEOF:
354 if (!lexrc.next()) break;
355 col.r = lexrc.getInteger();
357 if (!lexrc.next()) break;
358 col.g = lexrc.getInteger();
360 if (!lexrc.next()) break;
361 col.b = lexrc.getInteger();
363 fl_mapcolor(le, col.r, col.g, col.b);
370 bool XformsColor::write(string const & filename)
372 ofstream os(filename.c_str());
376 os << "### This file is part of\n"
377 << "### ========================================================\n"
378 << "### LyX, The Document Processor\n"
380 << "### Copyright 1995 Matthias Ettrich\n"
381 << "### Copyright 1995-2001 The LyX Team.\n"
383 << "### ========================================================\n"
385 << "# This file is written by LyX, if you want to make your own\n"
386 << "# modifications you should do them from inside LyX and save\n"
389 for (int i = 0; i < xformCount; ++i) {
390 string const tag = xformTags[i].tag;
391 int const colorID = xformTags[i].code;
394 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
397 << color.r << " " << color.g << " " << color.b << "\n";
404 string RWInfo::error_message;
406 bool RWInfo::WriteableDir(string const & name)
408 error_message.erase();
410 if (!AbsolutePath(name)) {
411 error_message = N_("The absolute path is required.");
415 FileInfo const tp(name);
416 if (!tp.isOK() || !tp.isDir()) {
417 error_message = N_("Directory does not exist.");
421 if (!tp.writable()) {
422 error_message = N_("Cannot write to this directory.");
430 bool RWInfo::ReadableDir(string const & name)
432 error_message.erase();
434 if (!AbsolutePath(name)) {
435 error_message = N_("The absolute path is required.");
439 FileInfo const tp(name);
440 if (!tp.isOK() || !tp.isDir()) {
441 error_message = N_("Directory does not exist.");
445 if (!tp.readable()) {
446 error_message = N_("Cannot read this directory.");
454 bool RWInfo::WriteableFile(string const & name)
456 // A writeable file is either:
457 // * An existing file to which we have write access, or
458 // * A file that doesn't yet exist but that would exist in a writeable
461 error_message.erase();
464 error_message = N_("No file input.");
468 string const dir = OnlyPath(name);
469 if (!AbsolutePath(dir)) {
470 error_message = N_("The absolute path is required.");
476 if (!d.isOK() || !d.isDir()) {
480 if (!d.isOK() || !d.isDir()) {
481 error_message = N_("Directory does not exist.");
486 error_message = N_("Cannot write to this directory.");
491 if (dir == name || (f.isOK() && f.isDir())) {
492 error_message = N_("A file is required, not a directory.");
496 if (f.isOK() && f.exist() && !f.writable()) {
497 error_message = N_("Cannot write to this file.");
505 bool RWInfo::ReadableFile(string const & name)
507 error_message.erase();
510 error_message = N_("No file input.");
514 string const dir = OnlyPath(name);
515 if (!AbsolutePath(dir)) {
516 error_message = N_("The absolute path is required.");
522 if (!d.isOK() && !d.isDir()) {
526 if (!d.isOK() || !d.isDir()) {
527 error_message = N_("Directory does not exist.");
532 error_message = N_("Cannot read from this directory.");
537 if (dir == name || (f.isOK() && f.isDir())) {
538 error_message = N_("A file is required, not a directory.");
543 error_message = N_("File does not exist.");
548 error_message = N_("Cannot read from this file.");