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 // old code which can be deleted if the new one, now enabled,
175 // works satisfyingly (JSpitzm, 11/02/2002)
176 // this should definitely be the other way around!!!
177 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
178 LyXLength const & len,
179 string const & default_unit)
182 updateWidgetsFromLengthString(input, choice,
183 string(), default_unit);
184 // use input field only for gluelengths
185 else if (!isValidLength(len) && !isStrDbl(len)) {
186 fl_set_input(input, len.c_str());
187 fl_set_choice_text(choice, default_unit.c_str());
190 updateWidgetsFromLengthString(input, choice,
191 len.asString(), default_unit);
196 // Most of the code here is a poor duplication of the parser code
197 // which is in LyXLength. Use that instead
198 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
200 string const & default_unit)
203 lyx::Assert(input && input->objclass == FL_INPUT &&
204 choice && choice->objclass == FL_CHOICE);
207 fl_set_input(input, "");
208 int unitpos = 1; // xforms has Fortran-style indexing
209 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
210 string const text = fl_get_choice_item_text(choice,i+1);
212 lowercase(strip(frontStrip(text)))) {
217 fl_set_choice(choice, unitpos);
221 // The unit is presumed to begin at the first char a-z
222 // or with the char '%'
223 string const tmp = lowercase(strip(frontStrip(str)));
225 string::const_iterator p = tmp.begin();
226 for (; p != tmp.end(); ++p) {
227 if ((*p >= 'a' && *p <= 'z') || *p == '%')
232 int unitpos = 1; // xforms has Fortran-style indexing
234 if (p == tmp.end()) {
239 string tmplen = string(tmp.begin(), p);
240 if (isStrDbl(tmplen))
242 string unit = string(p, tmp.end());
243 unit = subst(unit, "%", "%%");
245 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
246 string const text = fl_get_choice_item_text(choice,i+1);
247 if (unit == lowercase(strip(frontStrip(text)))) {
254 fl_set_input(input, len.c_str());
255 fl_set_choice(choice, unitpos);
258 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
260 string const & default_unit)
262 // use input field only for gluelengths
263 if (!isValidLength(str) && !isStrDbl(str)) {
264 fl_set_input(input, str.c_str());
265 fl_set_choice_text(choice, default_unit.c_str());
267 updateWidgetsFromLength(input, choice,
268 LyXLength(str), default_unit);
273 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
274 LyXLength const & len,
275 string const & default_unit)
278 lyx::Assert(input && input->objclass == FL_INPUT &&
279 choice && choice->objclass == FL_CHOICE);
282 fl_set_input(input, "");
283 fl_set_choice_text(choice, default_unit.c_str());
285 ostringstream buffer;
286 buffer << len.value();
287 fl_set_input(input, buffer.str().c_str());
288 fl_set_choice_text(choice,
289 subst(stringFromUnit(len.unit()),"%","%%").c_str());
295 // Take a string and add breaks so that it fits into a desired label width, w
296 string formatted(string const & sin, int w, int size, int style)
298 // FIX: Q: Why cant this be done by a one pass algo? (Lgb)
301 if (sin.empty()) return sout;
303 // breaks in up into a vector of individual words
304 vector<string> sentence;
306 for (string::const_iterator sit = sin.begin();
307 sit != sin.end(); ++sit) {
308 if ((*sit) == ' ' || (*sit) == '\n') {
310 sentence.push_back(word);
313 if ((*sit) == '\n') word += '\n';
320 // Flush remaining contents of word
321 if (!word.empty() ) sentence.push_back(word);
324 string line_plus_word;
325 for (vector<string>::const_iterator vit = sentence.begin();
326 vit != sentence.end(); ++vit) {
333 line_plus_word.erase();
337 if (!line_plus_word.empty() ) line_plus_word += ' ';
338 line_plus_word += word;
340 int const length = fl_get_string_width(style, size,
341 line_plus_word.c_str(),
342 int(line_plus_word.length()));
345 line_plus_word = word;
348 line = line_plus_word;
350 // Flush remaining contents of line
355 if (sout[sout.length() - 1] == '\n')
356 sout.erase(sout.length() - 1);
364 // sorted by hand to prevent LyXLex from complaining on read().
365 keyword_item xformTags[] = {
366 { "\\gui_background", FL_COL1 },
367 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
368 { "\\gui_buttonleft", FL_LEFT_BCOL },
369 { "\\gui_buttonright", FL_RIGHT_BCOL },
370 { "\\gui_buttontop", FL_TOP_BCOL },
371 { "\\gui_inactive", FL_INACTIVE },
372 { "\\gui_push_button", FL_YELLOW },
373 { "\\gui_selected", FL_MCOL },
374 { "\\gui_text", FL_BLACK }
378 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
383 bool XformsColor::read(string const & filename)
385 LyXLex lexrc(xformTags, xformCount);
386 if (!lexrc.setFile(filename))
389 while (lexrc.isOK()) {
390 int const le = lexrc.lex();
393 case LyXLex::LEX_UNDEF:
394 lexrc.printError("Unknown tag `$$Token'");
396 case LyXLex::LEX_FEOF:
403 if (!lexrc.next()) break;
404 col.r = lexrc.getInteger();
406 if (!lexrc.next()) break;
407 col.g = lexrc.getInteger();
409 if (!lexrc.next()) break;
410 col.b = lexrc.getInteger();
412 fl_mapcolor(le, col.r, col.g, col.b);
419 bool XformsColor::write(string const & filename)
421 ofstream os(filename.c_str());
425 os << "### This file is part of\n"
426 << "### ========================================================\n"
427 << "### LyX, The Document Processor\n"
429 << "### Copyright 1995 Matthias Ettrich\n"
430 << "### Copyright 1995-2001 The LyX Team.\n"
432 << "### ========================================================\n"
434 << "# This file is written by LyX, if you want to make your own\n"
435 << "# modifications you should do them from inside LyX and save\n"
438 for (int i = 0; i < xformCount; ++i) {
439 string const tag = xformTags[i].tag;
440 int const colorID = xformTags[i].code;
443 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
446 << color.r << " " << color.g << " " << color.b << "\n";
453 string RWInfo::error_message;
455 bool RWInfo::WriteableDir(string const & name)
457 error_message.erase();
459 if (!AbsolutePath(name)) {
460 error_message = N_("The absolute path is required.");
464 FileInfo const tp(name);
465 if (!tp.isOK() || !tp.isDir()) {
466 error_message = N_("Directory does not exist.");
470 if (!tp.writable()) {
471 error_message = N_("Cannot write to this directory.");
479 bool RWInfo::ReadableDir(string const & name)
481 error_message.erase();
483 if (!AbsolutePath(name)) {
484 error_message = N_("The absolute path is required.");
488 FileInfo const tp(name);
489 if (!tp.isOK() || !tp.isDir()) {
490 error_message = N_("Directory does not exist.");
494 if (!tp.readable()) {
495 error_message = N_("Cannot read this directory.");
503 bool RWInfo::WriteableFile(string const & name)
505 // A writeable file is either:
506 // * An existing file to which we have write access, or
507 // * A file that doesn't yet exist but that would exist in a writeable
510 error_message.erase();
513 error_message = N_("No file input.");
517 string const dir = OnlyPath(name);
518 if (!AbsolutePath(dir)) {
519 error_message = N_("The absolute path is required.");
525 if (!d.isOK() || !d.isDir()) {
529 if (!d.isOK() || !d.isDir()) {
530 error_message = N_("Directory does not exist.");
535 error_message = N_("Cannot write to this directory.");
540 if (dir == name || (f.isOK() && f.isDir())) {
541 error_message = N_("A file is required, not a directory.");
545 if (f.isOK() && f.exist() && !f.writable()) {
546 error_message = N_("Cannot write to this file.");
554 bool RWInfo::ReadableFile(string const & name)
556 error_message.erase();
559 error_message = N_("No file input.");
563 string const dir = OnlyPath(name);
564 if (!AbsolutePath(dir)) {
565 error_message = N_("The absolute path is required.");
571 if (!d.isOK() && !d.isDir()) {
575 if (!d.isOK() || !d.isDir()) {
576 error_message = N_("Directory does not exist.");
581 error_message = N_("Cannot read from this directory.");
586 if (dir == name || (f.isOK() && f.isDir())) {
587 error_message = N_("A file is required, not a directory.");
592 error_message = N_("File does not exist.");
597 error_message = N_("Cannot read from this file.");