2 * \file xforms_helpers.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS
14 #pragma implementation
17 #include "xforms_helpers.h"
21 #include "lyxlength.h"
22 #include "lyxgluelength.h"
24 #include "support/LAssert.h"
25 #include "support/FileInfo.h"
26 #include "support/filetools.h"
27 #include "support/lstrings.h" // frontStrip, strip
32 #include FORMS_H_LOCATION
38 // Set an FL_OBJECT to activated or deactivated
39 void setEnabled(FL_OBJECT * ob, bool enable)
42 fl_activate_object(ob);
43 fl_set_object_lcol(ob, FL_BLACK);
45 fl_deactivate_object(ob);
46 fl_set_object_lcol(ob, FL_INACTIVE);
51 // Given an fl_choice or an fl_browser, create a vector of its entries
52 vector<string> const getVector(FL_OBJECT * ob)
56 switch (ob->objclass) {
58 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
59 string const text = fl_get_choice_item_text(ob, i+1);
60 vec.push_back(trim(text));
64 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
65 string const text = fl_get_browser_line(ob, i+1);
66 vec.push_back(trim(text));
78 string const getString(FL_OBJECT * ob, int line)
82 switch (ob->objclass) {
84 lyx::Assert(line == -1);
85 tmp = fl_get_input(ob);
89 line = fl_get_browser(ob);
91 if (line >= 1 && line <= fl_get_browser_maxline(ob))
92 tmp = fl_get_browser_line(ob, line);
97 line = fl_get_choice(ob);
99 if (line >= 1 && line <= fl_get_choice_maxitems(ob))
100 tmp = fl_get_choice_item_text(ob, line);
107 return (tmp) ? trim(tmp) : string();
110 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
113 lyx::Assert(input && input->objclass == FL_INPUT &&
114 choice && choice->objclass == FL_CHOICE);
116 string const length = trim(fl_get_input(input));
120 //don't return unit-from-choice if the input(field) contains a unit
121 if (isValidGlueLength(length))
124 string unit = trim(fl_get_choice_text(choice));
125 unit = subst(unit, "%%", "%");
127 return length + unit;
131 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
133 string const & default_unit)
135 // use input field only for gluelengths
136 if (!isValidLength(str) && !isStrDbl(str)) {
137 fl_set_input(input, str.c_str());
138 // we assume that "default_unit" is in the choice as "we"
139 // have control over that!
140 // No need to check for its presence in the choice, therefore.
141 fl_set_choice_text(choice, default_unit.c_str());
143 updateWidgetsFromLength(input, choice,
144 LyXLength(str), default_unit);
149 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
150 LyXLength const & len,
151 string const & default_unit)
154 lyx::Assert(input && input->objclass == FL_INPUT &&
155 choice && choice->objclass == FL_CHOICE);
158 fl_set_input(input, "");
159 fl_set_choice_text(choice, default_unit.c_str());
161 ostringstream buffer;
162 buffer << len.value();
163 fl_set_input(input, buffer.str().c_str());
165 // Set the choice to the desired unit, if present in the choice.
166 // Else set the choice to the default unit.
167 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
169 vector<string> const vec = getVector(choice);
170 vector<string>::const_iterator it =
171 std::find(vec.begin(), vec.end(), unit);
172 if (it != vec.end()) {
173 fl_set_choice_text(choice, unit.c_str());
175 fl_set_choice_text(choice, default_unit.c_str());
181 // Take a string and add breaks so that it fits into a desired label width, w
182 string formatted(string const & sin, int w, int size, int style)
185 if (sin.empty()) return sout;
187 string::size_type curpos = 0;
190 string::size_type const nxtpos1 = sin.find(' ', curpos);
191 string::size_type const nxtpos2 = sin.find('\n', curpos);
192 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
194 string const word = nxtpos == string::npos ?
195 sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
197 bool const newline = (nxtpos2 != string::npos &&
200 string const line_plus_word =
201 line.empty() ? word : line + ' ' + word;
204 fl_get_string_width(style, size,
205 line_plus_word.c_str(),
206 int(line_plus_word.length()));
217 } else if (newline) {
218 sout += line_plus_word + '\n';
227 if (nxtpos == string::npos) {
240 void setCursorColor(int color)
242 fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
243 fl_set_cursor_color(XC_xterm, color, FL_WHITE);
244 fl_set_cursor_color(XC_watch, color, FL_WHITE);
245 fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
251 // sorted by hand to prevent LyXLex from complaining on read().
252 keyword_item xformTags[] = {
253 { "\\gui_background", FL_COL1 },
254 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
255 { "\\gui_buttonleft", FL_LEFT_BCOL },
256 { "\\gui_buttonright", FL_RIGHT_BCOL },
257 { "\\gui_buttontop", FL_TOP_BCOL },
258 { "\\gui_inactive", FL_INACTIVE },
259 { "\\gui_pointer", FL_FREE_COL16 },
260 { "\\gui_push_button", FL_YELLOW },
261 { "\\gui_selected", FL_MCOL },
262 { "\\gui_text", FL_BLACK }
266 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
271 bool XformsColor::read(string const & filename)
273 LyXLex lexrc(xformTags, xformCount);
274 if (!lexrc.setFile(filename))
277 while (lexrc.isOK()) {
278 int const le = lexrc.lex();
281 case LyXLex::LEX_UNDEF:
282 lexrc.printError("Unknown tag `$$Token'");
284 case LyXLex::LEX_FEOF:
289 string const tag = lexrc.getString();
293 if (!lexrc.next()) break;
294 col.r = lexrc.getInteger();
296 if (!lexrc.next()) break;
297 col.g = lexrc.getInteger();
299 if (!lexrc.next()) break;
300 col.b = lexrc.getInteger();
302 fl_mapcolor(le, col.r, col.g, col.b);
304 if (tag == "\\gui_pointer") {
305 setCursorColor(FL_FREE_COL16);
313 bool XformsColor::write(string const & filename)
315 ofstream os(filename.c_str());
320 << "### file " << filename << "\n\n"
321 << "### This file is written by LyX, if you want to make your own\n"
322 << "### modifications you should do them from inside LyX and save\n"
325 for (int i = 0; i < xformCount; ++i) {
326 string const tag = xformTags[i].tag;
327 int const colorID = xformTags[i].code;
330 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
333 << color.r << " " << color.g << " " << color.b << "\n";
340 string RWInfo::error_message;
342 bool RWInfo::WriteableDir(string const & name)
344 error_message.erase();
346 if (!AbsolutePath(name)) {
347 error_message = _("The absolute path is required.");
351 FileInfo const tp(name);
352 if (!tp.isOK() || !tp.isDir()) {
353 error_message = _("Directory does not exist.");
357 if (!tp.writable()) {
358 error_message = _("Cannot write to this directory.");
366 bool RWInfo::ReadableDir(string const & name)
368 error_message.erase();
370 if (!AbsolutePath(name)) {
371 error_message = _("The absolute path is required.");
375 FileInfo const tp(name);
376 if (!tp.isOK() || !tp.isDir()) {
377 error_message = _("Directory does not exist.");
381 if (!tp.readable()) {
382 error_message = _("Cannot read this directory.");
390 bool RWInfo::WriteableFile(string const & name)
392 // A writeable file is either:
393 // * An existing file to which we have write access, or
394 // * A file that doesn't yet exist but that would exist in a writeable
397 error_message.erase();
400 error_message = _("No file input.");
404 string const dir = OnlyPath(name);
405 if (!AbsolutePath(dir)) {
406 error_message = _("The absolute path is required.");
412 if (!d.isOK() || !d.isDir()) {
416 if (!d.isOK() || !d.isDir()) {
417 error_message = _("Directory does not exist.");
422 error_message = _("Cannot write to this directory.");
427 if (dir == name || (f.isOK() && f.isDir())) {
428 error_message = _("A file is required, not a directory.");
432 if (f.isOK() && f.exist() && !f.writable()) {
433 error_message = _("Cannot write to this file.");
441 bool RWInfo::ReadableFile(string const & name)
443 error_message.erase();
446 error_message = _("No file input.");
450 string const dir = OnlyPath(name);
451 if (!AbsolutePath(dir)) {
452 error_message = _("The absolute path is required.");
458 if (!d.isOK() && !d.isDir()) {
462 if (!d.isOK() || !d.isDir()) {
463 error_message = _("Directory does not exist.");
468 error_message = _("Cannot read from this directory.");
473 if (dir == name || (f.isOK() && f.isDir())) {
474 error_message = _("A file is required, not a directory.");
479 error_message = _("File does not exist.");
484 error_message = _("Cannot read from this file.");