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_input, return its contents.
94 string const getStringFromInput(FL_OBJECT * ob)
96 if (!ob || ob->objclass != FL_INPUT)
99 char const * tmp = fl_get_input(ob);
100 return (tmp) ? tmp : string();
104 // Given an fl_browser, return the contents of line
105 string const getStringFromBrowser(FL_OBJECT * ob, int line)
107 if (!ob || ob->objclass != FL_BROWSER ||
108 line < 1 || line > fl_get_browser_maxline(ob))
111 char const * tmp = fl_get_browser_line(ob, line);
112 return (tmp) ? tmp : string();
115 // Given an fl_browser, return the contents of the currently
117 // If nothing is selected, return an empty string
118 string const getSelectedStringFromBrowser(FL_OBJECT * ob)
120 if (!ob || ob->objclass != FL_BROWSER)
123 int const line = fl_get_browser(ob);
124 if (line < 1 || line > fl_get_browser_maxline(ob))
127 if (!fl_isselected_browser_line(ob, line))
130 char const * tmp = fl_get_browser_line(ob, line);
131 return (tmp) ? tmp : string();
135 // Given an fl_browser, create a vector of its entries
136 vector<string> const getVectorFromBrowser(FL_OBJECT * ob)
139 if (!ob || ob->objclass != FL_BROWSER)
142 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
143 string const text = fl_get_browser_line(ob, i+1);
144 vec.push_back(strip(frontStrip(text)));
151 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
154 lyx::Assert(input && input->objclass == FL_INPUT &&
155 choice && choice->objclass == FL_CHOICE);
157 string const length = strip(frontStrip(fl_get_input(input)));
161 string unit = strip(frontStrip(fl_get_choice_text(choice)));
162 unit = subst(unit, "%%", "%");
164 return length + unit;
169 // this should definitely be the other way around!!!
170 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
171 LyXLength const & len,
172 string const & default_unit)
175 updateWidgetsFromLengthString(input, choice,
176 string(), default_unit);
178 updateWidgetsFromLengthString(input, choice,
179 len.asString(), default_unit);
184 // Most of the code here is a poor duplication of the parser code
185 // which is in LyXLength. Use that instead
186 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
188 string const & default_unit)
191 lyx::Assert(input && input->objclass == FL_INPUT &&
192 choice && choice->objclass == FL_CHOICE);
195 fl_set_input(input, "");
196 int unitpos = 1; // xforms has Fortran-style indexing
197 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
198 string const text = fl_get_choice_item_text(choice,i+1);
200 lowercase(strip(frontStrip(text)))) {
205 fl_set_choice(choice, unitpos);
209 // The unit is presumed to begin at the first char a-z
210 string const tmp = lowercase(strip(frontStrip(str)));
212 string::const_iterator p = tmp.begin();
213 for (; p != tmp.end(); ++p) {
214 if (*p >= 'a' && *p <= 'z')
219 int unitpos = 1; // xforms has Fortran-style indexing
221 if (p == tmp.end()) {
226 string tmplen = string(tmp.begin(), p);
227 if (isStrDbl(tmplen))
229 string unit = string(p, tmp.end());
230 unit = subst(unit, "%", "%%");
232 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
233 string const text = fl_get_choice_item_text(choice,i+1);
234 if (unit == lowercase(strip(frontStrip(text)))) {
241 fl_set_input(input, len.c_str());
242 fl_set_choice(choice, unitpos);
245 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
247 string const & default_unit)
249 updateWidgetsFromLength(input, choice,
250 LyXLength(str), default_unit);
254 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
255 LyXLength const & len,
256 string const & default_unit)
259 lyx::Assert(input && input->objclass == FL_INPUT &&
260 choice && choice->objclass == FL_CHOICE);
263 fl_set_input(input, "");
264 fl_set_choice_text(choice, default_unit.c_str());
266 ostringstream buffer;
267 buffer << len.value();
268 fl_set_input(input, buffer.str().c_str());
269 fl_set_choice_text(choice, stringFromUnit(len.unit()));
275 // Take a string and add breaks so that it fits into a desired label width, w
276 string formatted(string const & sin, int w, int size, int style)
278 // FIX: Q: Why cant this be done by a one pass algo? (Lgb)
281 if (sin.empty()) return sout;
283 // breaks in up into a vector of individual words
284 vector<string> sentence;
286 for (string::const_iterator sit = sin.begin();
287 sit != sin.end(); ++sit) {
288 if ((*sit) == ' ' || (*sit) == '\n') {
290 sentence.push_back(word);
293 if ((*sit) == '\n') word += '\n';
300 // Flush remaining contents of word
301 if (!word.empty() ) sentence.push_back(word);
304 string line_plus_word;
305 for (vector<string>::const_iterator vit = sentence.begin();
306 vit != sentence.end(); ++vit) {
313 line_plus_word.erase();
317 if (!line_plus_word.empty() ) line_plus_word += ' ';
318 line_plus_word += word;
320 int const length = fl_get_string_width(style, size,
321 line_plus_word.c_str(),
322 int(line_plus_word.length()));
325 line_plus_word = word;
328 line = line_plus_word;
330 // Flush remaining contents of line
335 if (sout[sout.length() - 1] == '\n')
336 sout.erase(sout.length() - 1);
344 // sorted by hand to prevent LyXLex from complaining on read().
345 keyword_item xformTags[] = {
346 { "\\gui_background", FL_COL1 },
347 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
348 { "\\gui_buttonleft", FL_LEFT_BCOL },
349 { "\\gui_buttonright", FL_RIGHT_BCOL },
350 { "\\gui_buttontop", FL_TOP_BCOL },
351 { "\\gui_inactive", FL_INACTIVE },
352 { "\\gui_push_button", FL_YELLOW },
353 { "\\gui_selected", FL_MCOL },
354 { "\\gui_text", FL_BLACK }
358 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
363 bool XformsColor::read(string const & filename)
365 LyXLex lexrc(xformTags, xformCount);
366 if (!lexrc.setFile(filename))
369 while (lexrc.isOK()) {
370 int const le = lexrc.lex();
373 case LyXLex::LEX_UNDEF:
374 lexrc.printError("Unknown tag `$$Token'");
376 case LyXLex::LEX_FEOF:
383 if (!lexrc.next()) break;
384 col.r = lexrc.getInteger();
386 if (!lexrc.next()) break;
387 col.g = lexrc.getInteger();
389 if (!lexrc.next()) break;
390 col.b = lexrc.getInteger();
392 fl_mapcolor(le, col.r, col.g, col.b);
399 bool XformsColor::write(string const & filename)
401 ofstream os(filename.c_str());
405 os << "### This file is part of\n"
406 << "### ========================================================\n"
407 << "### LyX, The Document Processor\n"
409 << "### Copyright 1995 Matthias Ettrich\n"
410 << "### Copyright 1995-2001 The LyX Team.\n"
412 << "### ========================================================\n"
414 << "# This file is written by LyX, if you want to make your own\n"
415 << "# modifications you should do them from inside LyX and save\n"
418 for (int i = 0; i < xformCount; ++i) {
419 string const tag = xformTags[i].tag;
420 int const colorID = xformTags[i].code;
423 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
426 << color.r << " " << color.g << " " << color.b << "\n";
433 string RWInfo::error_message;
435 bool RWInfo::WriteableDir(string const & name)
437 error_message.erase();
439 if (!AbsolutePath(name)) {
440 error_message = N_("The absolute path is required.");
444 FileInfo const tp(name);
445 if (!tp.isOK() || !tp.isDir()) {
446 error_message = N_("Directory does not exist.");
450 if (!tp.writable()) {
451 error_message = N_("Cannot write to this directory.");
459 bool RWInfo::ReadableDir(string const & name)
461 error_message.erase();
463 if (!AbsolutePath(name)) {
464 error_message = N_("The absolute path is required.");
468 FileInfo const tp(name);
469 if (!tp.isOK() || !tp.isDir()) {
470 error_message = N_("Directory does not exist.");
474 if (!tp.readable()) {
475 error_message = N_("Cannot read this directory.");
483 bool RWInfo::WriteableFile(string const & name)
485 // A writeable file is either:
486 // * An existing file to which we have write access, or
487 // * A file that doesn't yet exist but that would exist in a writeable
490 error_message.erase();
493 error_message = N_("No file input.");
497 string const dir = OnlyPath(name);
498 if (!AbsolutePath(dir)) {
499 error_message = N_("The absolute path is required.");
505 if (!d.isOK() || !d.isDir()) {
509 if (!d.isOK() || !d.isDir()) {
510 error_message = N_("Directory does not exist.");
515 error_message = N_("Cannot write to this directory.");
520 if (dir == name || (f.isOK() && f.isDir())) {
521 error_message = N_("A file is required, not a directory.");
525 if (f.isOK() && f.exist() && !f.writable()) {
526 error_message = N_("Cannot write to this file.");
534 bool RWInfo::ReadableFile(string const & name)
536 error_message.erase();
539 error_message = N_("No file input.");
543 string const dir = OnlyPath(name);
544 if (!AbsolutePath(dir)) {
545 error_message = N_("The absolute path is required.");
551 if (!d.isOK() && !d.isDir()) {
555 if (!d.isOK() || !d.isDir()) {
556 error_message = N_("Directory does not exist.");
561 error_message = N_("Cannot read from this directory.");
566 if (dir == name || (f.isOK() && f.isDir())) {
567 error_message = N_("A file is required, not a directory.");
572 error_message = N_("File does not exist.");
577 error_message = N_("Cannot read from this file.");