2 * \file xforms_helpers.C
3 * Copyright 2000-2002 The LyX Team.
4 * See the file COPYING.
6 * \author Angus Leeming, a.leeming@ic.ac.uk
11 #include FORMS_H_LOCATION
13 #include <fstream> // ofstream
17 #pragma implementation
20 #include "xforms_helpers.h"
22 #include "support/FileInfo.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h" // frontStrip, strip
26 #include "support/LAssert.h"
27 #include "lyxlength.h"
28 #include "lyxgluelength.h"
34 // Extract shortcut from <ident>|<shortcut> string
35 char const * flyx_shortcut_extract(char const * sc)
37 // Find '|' in the sc and return the string after that.
38 register char const * sd = sc;
39 while (sd[0]!= 0 && sd[0] != '|') ++sd;
43 //lyxerr << sd << endl;
50 // Extract identifier from <ident>|<shortcut> string
51 char const * flyx_ident_extract(char const * sc)
53 register char const * se = sc;
54 while (se[0]!= 0 && se[0] != '|') ++se;
56 if (se[0] == 0) return sc;
58 char * sb = new char[se - sc + 1];
60 register char const * sd = sc;
70 // Set an FL_OBJECT to activated or deactivated
71 void setEnabled(FL_OBJECT * ob, bool enable)
74 fl_activate_object(ob);
75 fl_set_object_lcol(ob, FL_BLACK);
77 fl_deactivate_object(ob);
78 fl_set_object_lcol(ob, FL_INACTIVE);
83 // Given an fl_choice, create a vector of its entries
84 vector<string> const getVectorFromChoice(FL_OBJECT * ob)
87 if (!ob || ob->objclass != FL_CHOICE)
90 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
91 string const text = fl_get_choice_item_text(ob, i+1);
92 vec.push_back(strip(frontStrip(text)));
99 /// Given an fl_input, return its contents.
100 string const getStringFromInput(FL_OBJECT * ob)
102 if (!ob || ob->objclass != FL_INPUT)
105 char const * tmp = fl_get_input(ob);
106 return (tmp) ? tmp : string();
110 // Given an fl_browser, return the contents of line
111 string const getStringFromBrowser(FL_OBJECT * ob, int line)
113 if (!ob || ob->objclass != FL_BROWSER ||
114 line < 1 || line > fl_get_browser_maxline(ob))
117 char const * tmp = fl_get_browser_line(ob, line);
118 return (tmp) ? tmp : string();
121 // Given an fl_browser, return the contents of the currently
123 // If nothing is selected, return an empty string
124 string const getSelectedStringFromBrowser(FL_OBJECT * ob)
126 if (!ob || ob->objclass != FL_BROWSER)
129 int const line = fl_get_browser(ob);
130 if (line < 1 || line > fl_get_browser_maxline(ob))
133 if (!fl_isselected_browser_line(ob, line))
136 char const * tmp = fl_get_browser_line(ob, line);
137 return (tmp) ? tmp : string();
141 // Given an fl_browser, create a vector of its entries
142 vector<string> const getVectorFromBrowser(FL_OBJECT * ob)
145 if (!ob || ob->objclass != FL_BROWSER)
148 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
149 string const text = fl_get_browser_line(ob, i+1);
150 vec.push_back(strip(frontStrip(text)));
157 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
160 lyx::Assert(input && input->objclass == FL_INPUT &&
161 choice && choice->objclass == FL_CHOICE);
163 string const length = strip(frontStrip(fl_get_input(input)));
167 //don't return unit-from-choice if the input(field) contains a unit
168 if (isValidGlueLength(length))
171 string unit = strip(frontStrip(fl_get_choice_text(choice)));
172 unit = subst(unit, "%%", "%");
174 return length + unit;
179 // old code which can be deleted if the new one, now enabled,
180 // works satisfyingly (JSpitzm, 11/02/2002)
181 // this should definitely be the other way around!!!
182 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
183 LyXLength const & len,
184 string const & default_unit)
187 updateWidgetsFromLengthString(input, choice,
188 string(), default_unit);
189 // use input field only for gluelengths
190 else if (!isValidLength(len) && !isStrDbl(len)) {
191 fl_set_input(input, len.c_str());
192 fl_set_choice_text(choice, default_unit.c_str());
195 updateWidgetsFromLengthString(input, choice,
196 len.asString(), default_unit);
201 // Most of the code here is a poor duplication of the parser code
202 // which is in LyXLength. Use that instead
203 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
205 string const & default_unit)
208 lyx::Assert(input && input->objclass == FL_INPUT &&
209 choice && choice->objclass == FL_CHOICE);
212 fl_set_input(input, "");
213 int unitpos = 1; // xforms has Fortran-style indexing
214 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
215 string const text = fl_get_choice_item_text(choice,i+1);
217 lowercase(strip(frontStrip(text)))) {
222 fl_set_choice(choice, unitpos);
226 // The unit is presumed to begin at the first char a-z
227 // or with the char '%'
228 string const tmp = lowercase(strip(frontStrip(str)));
230 string::const_iterator p = tmp.begin();
231 for (; p != tmp.end(); ++p) {
232 if ((*p >= 'a' && *p <= 'z') || *p == '%')
237 int unitpos = 1; // xforms has Fortran-style indexing
239 if (p == tmp.end()) {
244 string tmplen = string(tmp.begin(), p);
245 if (isStrDbl(tmplen))
247 string unit = string(p, tmp.end());
248 unit = subst(unit, "%", "%%");
250 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
251 string const text = fl_get_choice_item_text(choice,i+1);
252 if (unit == lowercase(strip(frontStrip(text)))) {
259 fl_set_input(input, len.c_str());
260 fl_set_choice(choice, unitpos);
263 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
265 string const & default_unit)
267 // use input field only for gluelengths
268 if (!isValidLength(str) && !isStrDbl(str)) {
269 fl_set_input(input, str.c_str());
270 fl_set_choice_text(choice, default_unit.c_str());
272 updateWidgetsFromLength(input, choice,
273 LyXLength(str), default_unit);
278 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
279 LyXLength const & len,
280 string const & default_unit)
283 lyx::Assert(input && input->objclass == FL_INPUT &&
284 choice && choice->objclass == FL_CHOICE);
287 fl_set_input(input, "");
288 fl_set_choice_text(choice, default_unit.c_str());
290 ostringstream buffer;
291 buffer << len.value();
292 fl_set_input(input, buffer.str().c_str());
293 fl_set_choice_text(choice,
294 subst(stringFromUnit(len.unit()),"%","%%").c_str());
300 // Take a string and add breaks so that it fits into a desired label width, w
301 string formatted(string const & sin, int w, int size, int style)
303 // FIX: Q: Why cant this be done by a one pass algo? (Lgb)
306 if (sin.empty()) return sout;
308 // breaks in up into a vector of individual words
309 vector<string> sentence;
311 for (string::const_iterator sit = sin.begin();
312 sit != sin.end(); ++sit) {
313 if ((*sit) == ' ' || (*sit) == '\n') {
315 sentence.push_back(word);
318 if ((*sit) == '\n') word += '\n';
325 // Flush remaining contents of word
326 if (!word.empty()) sentence.push_back(word);
329 string line_plus_word;
330 for (vector<string>::const_iterator vit = sentence.begin();
331 vit != sentence.end(); ++vit) {
338 line_plus_word.erase();
342 if (!line_plus_word.empty()) line_plus_word += ' ';
343 line_plus_word += word;
345 int const length = fl_get_string_width(style, size,
346 line_plus_word.c_str(),
347 int(line_plus_word.length()));
350 line_plus_word = word;
353 line = line_plus_word;
355 // Flush remaining contents of line
360 if (sout[sout.length() - 1] == '\n')
361 sout.erase(sout.length() - 1);
369 // sorted by hand to prevent LyXLex from complaining on read().
370 keyword_item xformTags[] = {
371 { "\\gui_background", FL_COL1 },
372 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
373 { "\\gui_buttonleft", FL_LEFT_BCOL },
374 { "\\gui_buttonright", FL_RIGHT_BCOL },
375 { "\\gui_buttontop", FL_TOP_BCOL },
376 { "\\gui_inactive", FL_INACTIVE },
377 { "\\gui_pointer", FL_FREE_COL16 },
378 { "\\gui_push_button", FL_YELLOW },
379 { "\\gui_selected", FL_MCOL },
380 { "\\gui_text", FL_BLACK }
384 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
389 bool XformsColor::read(string const & filename)
391 LyXLex lexrc(xformTags, xformCount);
392 if (!lexrc.setFile(filename))
395 while (lexrc.isOK()) {
396 int const le = lexrc.lex();
399 case LyXLex::LEX_UNDEF:
400 lexrc.printError("Unknown tag `$$Token'");
402 case LyXLex::LEX_FEOF:
407 string const tag = lexrc.getString();
411 if (!lexrc.next()) break;
412 col.r = lexrc.getInteger();
414 if (!lexrc.next()) break;
415 col.g = lexrc.getInteger();
417 if (!lexrc.next()) break;
418 col.b = lexrc.getInteger();
420 fl_mapcolor(le, col.r, col.g, col.b);
422 if (tag == "\\gui_pointer") {
423 fl_set_cursor_color(FL_DEFAULT_CURSOR,
424 FL_FREE_COL16, FL_WHITE);
425 fl_set_cursor_color(XC_question_arrow,
426 FL_FREE_COL16, FL_WHITE);
434 bool XformsColor::write(string const & filename)
436 ofstream os(filename.c_str());
440 os << "### This file is part of\n"
441 << "### ========================================================\n"
442 << "### LyX, The Document Processor\n"
444 << "### Copyright 1995 Matthias Ettrich\n"
445 << "### Copyright 1995-2002 The LyX Team.\n"
447 << "### ========================================================\n"
449 << "# This file is written by LyX, if you want to make your own\n"
450 << "# modifications you should do them from inside LyX and save\n"
453 for (int i = 0; i < xformCount; ++i) {
454 string const tag = xformTags[i].tag;
455 int const colorID = xformTags[i].code;
458 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
461 << color.r << " " << color.g << " " << color.b << "\n";
468 string RWInfo::error_message;
470 bool RWInfo::WriteableDir(string const & name)
472 error_message.erase();
474 if (!AbsolutePath(name)) {
475 error_message = N_("The absolute path is required.");
479 FileInfo const tp(name);
480 if (!tp.isOK() || !tp.isDir()) {
481 error_message = N_("Directory does not exist.");
485 if (!tp.writable()) {
486 error_message = N_("Cannot write to this directory.");
494 bool RWInfo::ReadableDir(string const & name)
496 error_message.erase();
498 if (!AbsolutePath(name)) {
499 error_message = N_("The absolute path is required.");
503 FileInfo const tp(name);
504 if (!tp.isOK() || !tp.isDir()) {
505 error_message = N_("Directory does not exist.");
509 if (!tp.readable()) {
510 error_message = N_("Cannot read this directory.");
518 bool RWInfo::WriteableFile(string const & name)
520 // A writeable file is either:
521 // * An existing file to which we have write access, or
522 // * A file that doesn't yet exist but that would exist in a writeable
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 write to this directory.");
555 if (dir == name || (f.isOK() && f.isDir())) {
556 error_message = N_("A file is required, not a directory.");
560 if (f.isOK() && f.exist() && !f.writable()) {
561 error_message = N_("Cannot write to this file.");
569 bool RWInfo::ReadableFile(string const & name)
571 error_message.erase();
574 error_message = N_("No file input.");
578 string const dir = OnlyPath(name);
579 if (!AbsolutePath(dir)) {
580 error_message = N_("The absolute path is required.");
586 if (!d.isOK() && !d.isDir()) {
590 if (!d.isOK() || !d.isDir()) {
591 error_message = N_("Directory does not exist.");
596 error_message = N_("Cannot read from this directory.");
601 if (dir == name || (f.isOK() && f.isDir())) {
602 error_message = N_("A file is required, not a directory.");
607 error_message = N_("File does not exist.");
612 error_message = N_("Cannot read from this file.");