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, create a vector of its entries
94 vector<string> const getVectorFromBrowser(FL_OBJECT * ob)
97 if (!ob || ob->objclass != FL_BROWSER)
100 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
101 string const text = fl_get_browser_line(ob, i+1);
102 vec.push_back(strip(frontStrip(text)));
109 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
112 lyx::Assert(input && input->objclass == FL_INPUT &&
113 choice && choice->objclass == FL_CHOICE);
115 string const length = strip(frontStrip(fl_get_input(input)));
119 string unit = strip(frontStrip(fl_get_choice_text(choice)));
120 unit = subst(unit, "%%", "%");
122 return length + unit;
127 // this should definitely be the other way around!!!
128 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
129 LyXLength const & len,
130 string const & default_unit)
133 updateWidgetsFromLengthString(input, choice,
134 string(), default_unit);
136 updateWidgetsFromLengthString(input, choice,
137 len.asString(), default_unit);
142 // Most of the code here is a poor duplication of the parser code
143 // which is in LyXLength. Use that instead
144 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
146 string const & default_unit)
149 lyx::Assert(input && input->objclass == FL_INPUT &&
150 choice && choice->objclass == FL_CHOICE);
153 fl_set_input(input, "");
154 int unitpos = 1; // xforms has Fortran-style indexing
155 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
156 string const text = fl_get_choice_item_text(choice,i+1);
158 lowercase(strip(frontStrip(text)))) {
163 fl_set_choice(choice, unitpos);
167 // The unit is presumed to begin at the first char a-z
168 string const tmp = lowercase(strip(frontStrip(str)));
170 string::const_iterator p = tmp.begin();
171 for (; p != tmp.end(); ++p) {
172 if (*p >= 'a' && *p <= 'z')
177 int unitpos = 1; // xforms has Fortran-style indexing
179 if (p == tmp.end()) {
184 string tmplen = string(tmp.begin(), p);
185 if (isStrDbl(tmplen))
187 string unit = string(p, tmp.end());
188 unit = subst(unit, "%", "%%");
190 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
191 string const text = fl_get_choice_item_text(choice,i+1);
192 if (unit == lowercase(strip(frontStrip(text)))) {
199 fl_set_input(input, len.c_str());
200 fl_set_choice(choice, unitpos);
203 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
205 string const & default_unit)
207 updateWidgetsFromLength(input, choice,
208 LyXLength(str), default_unit);
212 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
213 LyXLength const & len,
214 string const & default_unit)
217 lyx::Assert(input && input->objclass == FL_INPUT &&
218 choice && choice->objclass == FL_CHOICE);
221 fl_set_input(input, "");
222 fl_set_choice_text(choice, default_unit.c_str());
224 ostringstream buffer;
225 buffer << len.value();
226 fl_set_input(input, buffer.str().c_str());
227 fl_set_choice_text(choice, stringFromUnit(len.unit()));
233 // Take a string and add breaks so that it fits into a desired label width, w
234 string formatted(string const & sin, int w, int size, int style)
236 // FIX: Q: Why cant this be done by a one pass algo? (Lgb)
239 if (sin.empty()) return sout;
241 // breaks in up into a vector of individual words
242 vector<string> sentence;
244 for (string::const_iterator sit = sin.begin();
245 sit != sin.end(); ++sit) {
246 if ((*sit) == ' ' || (*sit) == '\n') {
248 sentence.push_back(word);
251 if ((*sit) == '\n') word += '\n';
258 // Flush remaining contents of word
259 if (!word.empty() ) sentence.push_back(word);
262 string line_plus_word;
263 for (vector<string>::const_iterator vit = sentence.begin();
264 vit != sentence.end(); ++vit) {
271 line_plus_word.erase();
275 if (!line_plus_word.empty() ) line_plus_word += ' ';
276 line_plus_word += word;
278 int const length = fl_get_string_width(style, size,
279 line_plus_word.c_str(),
280 int(line_plus_word.length()));
283 line_plus_word = word;
286 line = line_plus_word;
288 // Flush remaining contents of line
293 if (sout[sout.length() - 1] == '\n')
294 sout.erase(sout.length() - 1);
302 // sorted by hand to prevent LyXLex from complaining on read().
303 keyword_item xformTags[] = {
304 { "\\gui_background", FL_COL1 },
305 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
306 { "\\gui_buttonleft", FL_LEFT_BCOL },
307 { "\\gui_buttonright", FL_RIGHT_BCOL },
308 { "\\gui_buttontop", FL_TOP_BCOL },
309 { "\\gui_inactive", FL_INACTIVE },
310 { "\\gui_push_button", FL_YELLOW },
311 { "\\gui_selected", FL_MCOL },
312 { "\\gui_text", FL_BLACK }
316 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
321 bool XformsColor::read(string const & filename)
323 LyXLex lexrc(xformTags, xformCount);
324 if (!lexrc.setFile(filename))
327 while (lexrc.isOK()) {
328 int const le = lexrc.lex();
331 case LyXLex::LEX_UNDEF:
332 lexrc.printError("Unknown tag `$$Token'");
334 case LyXLex::LEX_FEOF:
341 if (!lexrc.next()) break;
342 col.r = lexrc.getInteger();
344 if (!lexrc.next()) break;
345 col.g = lexrc.getInteger();
347 if (!lexrc.next()) break;
348 col.b = lexrc.getInteger();
350 fl_mapcolor(le, col.r, col.g, col.b);
357 bool XformsColor::write(string const & filename)
359 ofstream os(filename.c_str());
363 os << "### This file is part of\n"
364 << "### ========================================================\n"
365 << "### LyX, The Document Processor\n"
367 << "### Copyright 1995 Matthias Ettrich\n"
368 << "### Copyright 1995-2001 The LyX Team.\n"
370 << "### ========================================================\n"
372 << "# This file is written by LyX, if you want to make your own\n"
373 << "# modifications you should do them from inside LyX and save\n"
376 for (int i = 0; i < xformCount; ++i) {
377 string const tag = xformTags[i].tag;
378 int const colorID = xformTags[i].code;
381 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
384 << color.r << " " << color.g << " " << color.b << "\n";
391 string RWInfo::error_message;
393 bool RWInfo::WriteableDir(string const & name)
395 error_message.erase();
397 if (!AbsolutePath(name)) {
398 error_message = N_("The absolute path is required.");
402 FileInfo const tp(name);
403 if (!tp.isOK() || !tp.isDir()) {
404 error_message = N_("Directory does not exist.");
408 if (!tp.writable()) {
409 error_message = N_("Cannot write to this directory.");
417 bool RWInfo::ReadableDir(string const & name)
419 error_message.erase();
421 if (!AbsolutePath(name)) {
422 error_message = N_("The absolute path is required.");
426 FileInfo const tp(name);
427 if (!tp.isOK() || !tp.isDir()) {
428 error_message = N_("Directory does not exist.");
432 if (!tp.readable()) {
433 error_message = N_("Cannot read this directory.");
441 bool RWInfo::WriteableFile(string const & name)
443 // A writeable file is either:
444 // * An existing file to which we have write access, or
445 // * A file that doesn't yet exist but that would exist in a writeable
448 error_message.erase();
451 error_message = N_("No file input.");
455 string const dir = OnlyPath(name);
456 if (!AbsolutePath(dir)) {
457 error_message = N_("The absolute path is required.");
463 if (!d.isOK() || !d.isDir()) {
467 if (!d.isOK() || !d.isDir()) {
468 error_message = N_("Directory does not exist.");
473 error_message = N_("Cannot write to this directory.");
478 if (dir == name || (f.isOK() && f.isDir())) {
479 error_message = N_("A file is required, not a directory.");
483 if (f.isOK() && f.exist() && !f.writable()) {
484 error_message = N_("Cannot write to this file.");
492 bool RWInfo::ReadableFile(string const & name)
494 error_message.erase();
497 error_message = N_("No file input.");
501 string const dir = OnlyPath(name);
502 if (!AbsolutePath(dir)) {
503 error_message = N_("The absolute path is required.");
509 if (!d.isOK() && !d.isDir()) {
513 if (!d.isOK() || !d.isDir()) {
514 error_message = N_("Directory does not exist.");
519 error_message = N_("Cannot read from this directory.");
524 if (dir == name || (f.isOK() && f.isDir())) {
525 error_message = N_("A file is required, not a directory.");
530 error_message = N_("File does not exist.");
535 error_message = N_("Cannot read from this file.");